两个打印语句,但只有一个显示用户输入的字符串。

huangapple go评论78阅读模式
英文:

Two print statements, but only one displays a user input string

问题

使用这段代码,我想从用户那里获取两种食物。带有food1的打印语句在控制台中正确打印和显示用户输入的字符串。
对于food2的打印语句,它会打印出该语句,但用户输入的字符串不会一起显示出来。
我尝试过使用print/println,但都没有改变任何情况。

for(x=1; x <= 2; x++)
    System.out.print("请输入食物的国籍: ");
    Nation = sc.nextLine();
    System.out.print("请输入第一种食物: ");
    food1 = sc.nextLine();
    System.out.print("请输入食物价格: ");
    value1 = sc.nextDouble();
    
    System.out.print("请输入第二种食物选择: ");
    food2 = sc.nextLine();
    sc.next();
    System.out.print("请输入食物价格: ");
    value2 = sc.nextDouble();
    total = (value1 + value2);
    System.out.println("您选择的食物国籍: " + Nation);
    System.out.printf("您的总价格是: %.2f\n", total);
    System.out.print("您是否想列出您的物品?(1代表是,2代表否)");
    choice = sc.nextInt();
    while( choice == 1)
   
      System.out.println("您的第一种食物: " + food1); // 这个在控制台中正确显示
      System.out.println("您的第二种食物: " + food2); // 这个却没有在控制台中显示用户输入的字符串
      choice++;
      sc.nextLine();
英文:

With this code, I'm looking to get 2 foods from the user. The print statement with food1 prints and displays the string the user entered correctly in the console.
With the print statement for food2, it will print the statement but the string that the user input will not display along with it.
I've tried using print/println neither change anything.

for(x=1; x &lt;= 2; x++)

System.out.print(&quot;Enter nationality of the food: &quot;);
Nation = sc.nextLine();
System.out.print(&quot;Enter your first food: &quot;);

food1 = sc.nextLine();
System.out.print(&quot;Enter food price: &quot;);
value1 = sc.nextDouble();

System.out.print(&quot;Enter your second food choice: &quot;);
food2 = sc.nextLine();
sc.next();
System.out.print(&quot;Enter food price: &quot;);
value2 = sc.nextDouble();
total = (value1 + value2);
System.out.println(&quot;Your food nationality: &quot; + Nation);
System.out.printf(&quot;Your total price is: %.2f\n&quot;, total);
System.out.print(&quot;Would you like to list your items? (1 For Yes/2 For No)&quot;);
choice = sc.nextInt();
while( choice == 1)

  System.out.println(&quot;Your first food: &quot; + food1); // This one displays correctly in the console 
  System.out.println(&quot;Your second food: &quot; + food2); // This one does not display the string that the user inputted in the console 
  choice++;
  sc.nextLine();

答案1

得分: 0

sc.nextDouble()只会获取下一个小数值,而不会获取整行内容。因此,在读取food2之前,您需要先读取该行。

System.out.print("输入第一个食物:");
food1 = sc.nextLine();
System.out.print("输入食物价格:");
value1 = sc.nextDouble();
sc.nextLine(); // 添加这一行

System.out.print("输入您的第二个食物选择:");
food2 = sc.nextLine();
sc.next(); // 我不确定您在这里做了什么。如果我正确理解您的代码,您可以删除这行
System.out.print("输入食物价格:");
value2 = sc.nextDouble();
sc.nextLine(); // 添加这一行
英文:

sc.nextDouble() does not take in the entire line, only the next decimal value. Therefore you will need to read in that line before reading in food2.

System.out.print(&quot;Enter your first food: &quot;);
food1 = sc.nextLine();
System.out.print(&quot;Enter food price: &quot;);
value1 = sc.nextDouble();
sc.nextLine(); // add this line

System.out.print(&quot;Enter your second food choice: &quot;);
food2 = sc.nextLine();
sc.next(); // I am not sure what you are doing here. If I understand your code correctly, you can delete this
System.out.print(&quot;Enter food price: &quot;);
value2 = sc.nextDouble();
sc.nextLine(); // add this line

huangapple
  • 本文由 发表于 2020年10月17日 05:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64396425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定