此程序将值添加到名为money的变量中,该变量最初设置为0,具体取决于生成的数字(素数,复合,正方形或其他)。
我知道如果我有两个相同的数字,那么它会一直产生0。 我几乎可以肯定11的复数,复合材料和素数总是产生正确的值。
例如,当我得到52(复合),73(素数),13(素数),56(复合),64(正方形)和23(素数)的随机数时,我得到的返回值为708 ,答案应该是716。
52 + 100 + 100 +56 = 308 * 2 = 616 + 100 = 716
正方形的循环一定有问题,任何人都可以看到任何可能无法产生正确结果的东西吗?谢谢!
//importing popup window and random numbers
import javax.swing.JOptionPane;
import java.util.Random;
public class HarvestProject3
{//open HarvestProject3 class
public static void main (String [] args)
{//open main
//Start screen
String name = JOptionPane.showInputDialog(null, "Hello, what is your name?");
System.out.println(name);
String charity = JOptionPane.showInputDialog(null, "Ok, " + name + " , instead of collecting candy on October 31st, you will be collecting money for your own charity. \n Now, please tell me what is the name of your charity that you would like to collect the money for?");
System.out.println(charity);
JOptionPane.showMessageDialog(null, "Great! I am going to pick 6 randomly selected houses (by house number) for you to stop by to collect money for " + charity + ". \n \n Pleas be aware that it is Halloween night, you don't what can happen, expecially on a street called Crazy Street.");
//Random Numbers
Random rangen = new Random();
int HouseNumber1 = rangen.nextInt(98) + 3;
int HouseNumber2 = rangen.nextInt(98) + 3;
int HouseNumber3 = rangen.nextInt(98) + 3;
int HouseNumber4 = rangen.nextInt(98) + 3;
int HouseNumber5 = rangen.nextInt(98) + 3;
int HouseNumber6 = rangen.nextInt(98) + 3;
JOptionPane.showMessageDialog(null, "Ok, " + name + ", you are going to be stopping at houses, " + HouseNumber1 + ", " + HouseNumber2 + ", " + HouseNumber3 + ", " + HouseNumber4 + ", " + HouseNumber5 + ", and " + HouseNumber6);
if (HouseNumber1 == HouseNumber2 || HouseNumber1 == HouseNumber3 || HouseNumber1 == HouseNumber4 || HouseNumber1 == HouseNumber5 || HouseNumber1 == HouseNumber6 || HouseNumber2 == HouseNumber3 || HouseNumber2 == HouseNumber4 || HouseNumber2 == HouseNumber5 || HouseNumber2 == HouseNumber6 || HouseNumber3 == HouseNumber4 || HouseNumber3 == HouseNumber5 || HouseNumber3 == HouseNumber6 || HouseNumber4 == HouseNumber5 || HouseNumber4 == HouseNumber6 || HouseNumber5 == HouseNumber6)
{//open if
int NoMoney = 0;
JOptionPane.showMessageDialog(null, "You have asked for money from the same house again... \n \n A nasty old lady opens the door and screams, Give me your money!, Get out of my house! and chased you off Crazy Street and stole all of your money. \n \n You collected " + NoMoney + " dollars.");
System.exit(0);
}//close if
int money = 0;
//Looping for primes
int[] PrimeArray = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
for (int i : PrimeArray)
{//open enhanced for loop
if (HouseNumber1 == i || HouseNumber2 == i || HouseNumber3 == i || HouseNumber4 == i || HouseNumber5 == i || HouseNumber6 == i)
{//open if
money += 100;
}//close if
}//close enhanced for loop
//Looping for squares
int SquareArray[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
for (int i : SquareArray)
{//open enhanced for loop
if (HouseNumber1 == i || HouseNumber2 == i || HouseNumber3 == i || HouseNumber4 == i || HouseNumber5 == i || HouseNumber6 == i)
{//open if
money *= 2;
}//close if
}//close enhanced for loop
//Looping for multiples of 11 (not including 11 because it is prime)
int ElevenArray[] = {22, 33, 44, 55, 66, 77, 88, 99};
for (int i : ElevenArray)
{//open enhanced for loop
if (HouseNumber1 == i || HouseNumber2 == i || HouseNumber3 == i || HouseNumber4 == i || HouseNumber5 == i || HouseNumber6 == i)
{//open if
money -= 5;
}//close if
}//close enhanced for loop
//Looping for some composites
int SomeCompositesArray[] = {6, 8, 10, 12, 14, 18, 20, 21, 24, 26, 27, 28, 30, 32, 34, 35, 38, 39, 40, 42, 45, 46, 48, 50, 51, 52, 54, 56, 57, 58, 60, 62, 63, 65, 68, 69, 70, 72, 74, 75, 76, 78, 80, 82, 84, 85, 86, 87, 90, 91, 92, 93, 94, 95, 96, 98};
for (int i : SomeCompositesArray)
{//open enhanced for loop
if (HouseNumber1 == i)
{//open if
money += HouseNumber1;
}//close if
if (HouseNumber2 == i)
{//open if
money += HouseNumber2;
}//close if
if (HouseNumber3 == i)
{//open if
money += HouseNumber3;
}//close if
if (HouseNumber4 == i)
{//open if
money += HouseNumber4;
}//close if
if (HouseNumber5 == i)
{//open if
money += HouseNumber5;
}//close if
if (HouseNumber6 == i)
{//open if
money += HouseNumber6;
}//close if
}//close enhanced for loop
JOptionPane.showMessageDialog(null, "You collected " + money + " dollars for " + charity + "!!!");
}//close main
}//close HarvestProject3 class
答案 0 :(得分:0)
我不知道你想要什么,但你不能这样做:
money *= 2;
或者
money += money;
而不是
money += DoubledMoney;
顺便说一句:为什么不在if中添加超过条件,而不是做100x ifs:
if(HouseNumber1 == i || HouseNumber2 == i || HouseNumberX == i){
// do the work
}
答案 1 :(得分:0)
您可以缩短一堆代码,以减少在很多地方重复出错的可能性。我也改了钱* = 2;这应该是你想要的。希望我在这个漫长的过程中没有弄乱你的任何东西嘿。附:尚未在IDE中测试过这个
String name = JOptionPane.showInputDialog(null, "Hello, what is your name?");
System.out.println(name);
String charity = JOptionPane.showInputDialog(null, "Ok, " + name + " , instead of collecting candy on October 31st, you will be collecting money for your own charity. \n Now, please tell me what is the name of your charity that you would like to collect the money for?");
System.out.println(charity);
JOptionPane.showMessageDialog(null, "Great! I am going to pick 6 randomly selected houses (by house number) for you to stop by to collect money for " + charity + ". \n \n Pleas be aware that it is Halloween night, you don't what can happen, expecially on a street called Crazy Street.");
Random rangen = new Random();
int[] houseNumbers = new int[6];
for(int i = 0; i < houseNumbers.length; i ++){
houseNumbers[i] = rangen.nextInt(98) + 2;
}
String housesString = "";
for(int i = 0; i < houseNumbers.length; i ++){
housesString += (i != houseNumbers.length - 1) ? houseNumbers[i] + ", " : " and " + houseNumbers[i];
}
JOptionPane.showMessageDialog(null, "Ok, " + name + ", you are going to be stopping at houses, " + housesString);
Set<Integer> lump = new HashSet<Integer>();
for (int i : houseNumbers){
if (lump.contains(i)){
int NoMoney = 0;
JOptionPane.showMessageDialog(null, "You have asked for money from the same house again... \n A mean old lady opens the door and screams, Give me your money!, Get out of my house! and chased you off Crazy Street and stole all of your money. \n You collected " + NoMoney + " dollars");
System.exit(0);
}
lump.add(i);
}
int money = 0;
int[] PrimeArray = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
for (int i : PrimeArray)
{
for(int j : houseNumbers){
if(j == i)
money += 100;
}
}
int SquareArray[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
for (int i : SquareArray)
{
for(int j : houseNumbers){
if(j == i)
money *= 2;
}
}
int ElevenArray[] = {22, 33, 44, 55, 66, 77, 88, 99};
for (int i : ElevenArray)
{
for(int j : houseNumbers){
if(j == i)
money -= 5;
}
}
int SomeCompositesArray[] = {6, 8, 10, 12, 14, 18, 20, 21, 24, 26, 27, 28, 30, 32, 34, 35, 38, 39, 40, 42, 45, 46, 48, 50, 51, 52, 54, 56, 57, 58, 60, 62, 63, 65, 68, 69, 70, 72, 74, 75, 76, 78, 80, 82, 84, 85, 86, 87, 90, 91, 92, 93, 94, 95, 96, 98};
for (int i : SomeCompositesArray)
{
for(int j : houseNumbers){
if(j == i)
money += j;
}
}
JOptionPane.showMessageDialog(null, "You collected " + money + " dollars for " + charity + "!!!");