我试图询问用户是否愿意在循环结束时选择Y或N继续。如果他们选择Y,它应该重新启动整个程序,如果N则应该结束。我尝试做的时候一直都会出错。
import javax.swing.JOptionPane;
public class killmenow2
{
static int killmenow2;
public static void main (String args[])
{
String stringFirstNum, stringNextNum, choice;
final String restart = 0;
double firstNum, numNums = 0, total, accumTotal = 0, average, nextNum;
do
{
//user enters first number
stringFirstNum = JOptionPane.showInputDialog("Enter the first number: ");
firstNum = Double.parseDouble(stringFirstNum); //convert string to int
``//Initialize accumulator variable to 0
for(numNums = 0; numNums>0; numNums++)
accumTotal = 0;
{
//user enters next number, begins loop which terminates when user enters "0"
nextNum = 1;
numNums = numNums++;
while (nextNum != 0)
{
stringNextNum = JOptionPane.showInputDialog("Enter the next number: ");
nextNum = Double.parseDouble(stringNextNum); //convert string to int
accumTotal += nextNum;
numNums++;
}
}
//Accumulate total of next numbers
total = accumTotal + firstNum;
System.out.println("The total of your numbers is: " + total);
//calculate average of numbers
average = total / numNums;
System.out.println("The average of your numbers is: " + average);
System.out.println("numnums is " + numNums);
//user inputs if they would like to continue processing numbers
System.out.println("Would you like to continue processing numbers? (Y/N)");
if (choice.equals("Y"))
restart = 0;
else restart = 1;
} //do loop close bracket
while (restart < 0);
System.out.println("Goodbye");
System.exit (0);
}
}
答案 0 :(得分:0)
final String restart = 0;
这是非法代码
这是
while (restart < 0);
String
与int
尝试使用像eclipse这样的IDE并阅读错误
答案 1 :(得分:0)
final String restart = 0;
删除final
关键字。然后将声明更改为
boolean restart = true;
然后改变逻辑条件
do {
...
System.out.println("Would you like to continue processing numbers? (Y/N)");
***** ADD CODE HERE TO READ THE RESPONSE *****
if (choice.equals("Y"))
restart = true;
else
restart = false;
} while (restart);
更好:
do {
...
System.out.println("Would you like to continue processing numbers? (Y/N)");
***** ADD CODE HERE TO READ THE RESPONSE *****
} while (choice.equalsIgnoreCase("Y"));