我有一个使用扫描仪读取数据的课程作业。
import java.util.Scanner;
public class Project23
{
public static void main(String[] args)
{
// Declarations and instantiations.
Scanner scan = new Scanner(System.in);
String any = "";
boolean more = false;
double purchase = 0.0;
// Ask if user would like to run program?
System.out.print("Do you have any purchases? Y/N?: ");
// Ready value into string.
any = scan.nextLine();
System.out.println();
// If any is equal to y or Y it will set the value of more to true
// this runs the while statement below.
more = any.toUpperCase().equals("Y");
// While more is still true continue to run the code within the brackets.
while (more)
{
System.out.print("Please input purchase amount: ");
purchase += scan.nextDouble();
System.out.println();
System.out.print("Do you have any more purchases Y/N?: ");
any = scan.nextLine();
System.out.println();
more = any.toUpperCase().equals("Y");
}
if (purchase >= 2500)
System.out.println("Purchase >= 2500");
else
System.out.println("Purchase < 2500");
}
}
底部只是作为测试让我了解一切是否正常。但是,我设置的 while 循环似乎不想继续运行不止一次。它将接受一个值,然后如果我说是,我有更多值(y 或 Y),它将退出并打印任一鲣鸟