java为什么我输入itemName后我的程序就不能继续了?
当我输入字符串“end”作为其中一项时,我不明白为什么我的程序不能继续通过while循环。我试着把println语句放在while循环之后,但在我输入“end”之后它们就不打印了。我还试着在while循环的末尾放一个print语句,但在我输入“end”后它就不打印了,所以在我输入“end”后它不会运行while循环,但在它之后它也不会运行任何东西。有什么想法吗?以下是代码:
package a1;
import java.util.Scanner;
public class A1Adept {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
process(s);
}
public static void process(Scanner s) {
int[] numbers = new int[10];
double[] cost = new double[10];
String itemName = "";
int categoryNumb;
int quantities;
double costs;
System.out.println("Please enter the names of the items, their category, their quantity, and their cost.");
while(!itemName.equals("end")){
itemName = s.next();
categoryNumb = s.nextInt();
quantities = s.nextInt();
costs = s.nextDouble();
numbers[categoryNumb] += quantities;
cost[categoryNumb] += (costs*quantities);
}
System.out.println("win");
int qMax = 0;
int qMin = 0;
int cLarge = 0;
int cLeast = 0;
int max = 0;
int min = 100;
double large = 0;
double least = 100;
for (int i = 0; i < 10; i++){
if (numbers[i] >= max)
{
max = numbers[i];
qMax = i;
}
if (numbers[i] <= min){
min = numbers[i];
qMin = i;
}
if (cost[i] >= large){
large = cost[i];
cLarge = i;
}
if (cost[i] <= least){
least = cost[i];
cLeast = i;
}
}
System.out.println("Category with the most items:"+qMax);
System.out.println("Category with the least items:"+qMin);
System.out.println("Category with the largest cost:"+cLarge);
System.out.println("Category with the least cost:"+cLeast);
}
}
# 1 楼答案
如果你写“end”,后跟一个int,另一个int和一个double,它就会停止
这是因为您首先检查“结束”,然后请求4个输入
while(条件)在每个循环开始时进行评估
所以你的程序是这样的:
如果要在用户键入“结束”后立即退出,请将其更改为: