我的Java中的while循环不起作用,只执行了一次

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

My while loop in java doesn't work and only completes it one time

问题

package Chaterp5PPReynaGuerra;
import java.util.*;

public class MeetingRequest 
{

    public static void main(String[] args) 
    {

        Scanner scan = new Scanner(System.in);

        final int CAPACITY = 30;
        String name;
        int people;
        int morePeople;
        String answer = "y";
        int fewerPeople;

        System.out.println("--------会议请求系统--------");

        System.out.println("\n欢迎使用会议请求系统。请问您的名字是什么?");
        name = scan.nextLine();


        while (answer.equalsIgnoreCase("y")) 
        {
            System.out.println("你好," + name + ",会议上会有多少人参加?");
            people = scan.nextInt();

            morePeople = CAPACITY - people;
            if (people < CAPACITY)
                System.out.println("你可以再邀请" + morePeople + "个人参加会议。");
            else if (people > CAPACITY) {
                fewerPeople = people - CAPACITY;
                System.out.println("抱歉,会议室的空间不足以容纳那么多人。你需要将" +
                        fewerPeople + "人排除在会议之外。");
            }
            System.out.println();

            System.out.println("您是否想发起另一个请求?(y/n)");
            // 清除输入流中的 \n
            scan.next();
            answer = scan.nextLine();
        }
    }
}
英文:

It will only go through one time so it will end with "Would you like to make another request?(y/n)"
and when I input "y" it stops there and won't do the loop.

package Chaterp5PPReynaGuerra;
import java.util.*;
public class MeetingRequest 
{
public static void main(String[] args) 
{
Scanner scan = new Scanner(System.in);
final int CAPACITY=30;
String name;
int people;
int morePeople;
String answer=&quot;y&quot;;
int fewerPeople;
System.out.println(&quot;--------Meeting Request System&quot;+
&quot;--------&quot;);
System.out.println(&quot;\nWelcome to the Meeting Request System.&quot;+
&quot; May I know your name?&quot;);
name=scan.nextLine();
while(answer.equalsIgnoreCase(&quot;y&quot;)) 
{
System.out.println(&quot;Hello, &quot;+name+&quot;, how many people&quot;+
&quot; will be attending the meeting?&quot;);
people=scan.nextInt();
morePeople = CAPACITY - people;
if(people &lt; CAPACITY)
System.out.println(&quot;You can invite &quot;+morePeople+
&quot; more people to the meeting.&quot;);
else if(people &gt; CAPACITY) {
fewerPeople= people - CAPACITY;
System.out.println(&quot;Sorry, the room is not &quot;+
&quot;big enough to seat that many people. You have to &quot;+
&quot;exclude &quot;+fewerPeople+&quot; from the meeting.&quot;);
}
System.out.println();
System.out.println(&quot;Would you like to make another&quot;+
&quot; request?(y /n)&quot;);
// gets rid of \n in the input stream
scan.next();
answer=scan.nextLine();
}
}
}

答案1

得分: 0

使用next()将仅返回分隔符(默认为空格)之前的内容。nextLine()在返回当前行后会自动移动扫描器。

要去掉\n,请使用scan.nextLine

// 在输入流中去掉 \n
            scan.nextLine();
            answer = scan.nextLine();

希望这对你有帮助。

英文:

Using next() will only return what comes before the delimiter (defaults to whitespace). nextLine() automatically moves the scanner down after returning the current line.

To get rid of the \n use scan.nextLine

// gets rid of \n in the input stream
scan.nextLine();
answer=scan.nextLine();

Hope this help.

答案2

得分: 0

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        final int CAPACITY = 30;
        String name;
        int people = 0;
        int morePeople;
        String answer = "y";
        int fewerPeople;
        boolean valid;
        System.out.println("--------Meeting Request System--------");

        System.out.println("\nWelcome to the Meeting Request System." + " May I know your name?");
        name = scan.nextLine();

        do {
            do {
                valid = true;
                System.out.println("Hello, " + name + ", how many people" + " will be attending the meeting?");
                try {
                    people = Integer.parseInt(scan.nextLine());
                } catch (NumberFormatException e) {
                    System.out.println("Invalid entry. Pleaase try again.");
                    valid = false;
                }
            } while (!valid);

            morePeople = CAPACITY - people;
            if (people < CAPACITY)
                System.out.println("You can invite " + morePeople + " more people to the meeting.");
            else if (people > CAPACITY) {
                fewerPeople = people - CAPACITY;
                System.out.println("Sorry, the room is not " + "big enough to seat that many people. You have to "
                        + "exclude " + fewerPeople + " from the meeting.");
            }
            System.out.println();

            System.out.println("Would you like to make another" + " request?(y /n)");
            answer = scan.nextLine();
        } while (answer.equalsIgnoreCase("y"));
    }
}

A sample run:

--------Meeting Request System--------

Welcome to the Meeting Request System. May I know your name?
abc
Hello, abc, how many people will be attending the meeting?
x
Invalid entry. Pleaase try again.
Hello, abc, how many people will be attending the meeting?
10.4
Invalid entry. Pleaase try again.
Hello, abc, how many people will be attending the meeting?
4
You can invite 26 more people to the meeting.

Would you like to make another request?(y /n)
y
Hello, abc, how many people will be attending the meeting?
5
You can invite 25 more people to the meeting.

Would you like to make another request?(y /n)
n

Some other important points:

  1. As you can see, a do...while loop is more appropriate instead of while loop in this case.
  2. You should always check for the NumberFormatException whenever you parse a text (e.g. Scanner::nextLine()) to integer.
英文:

Replace

people=scan.nextInt();

with

people = Integer.parseInt(scan.nextLine());

Check https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo to learn more about it.

Given below is the corrected program:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int CAPACITY = 30;
String name;
int people = 0;
int morePeople;
String answer = &quot;y&quot;;
int fewerPeople;
boolean valid;
System.out.println(&quot;--------Meeting Request System&quot; + &quot;--------&quot;);
System.out.println(&quot;\nWelcome to the Meeting Request System.&quot; + &quot; May I know your name?&quot;);
name = scan.nextLine();
do {
do {
valid = true;
System.out.println(&quot;Hello, &quot; + name + &quot;, how many people&quot; + &quot; will be attending the meeting?&quot;);
try {
people = Integer.parseInt(scan.nextLine());
} catch (NumberFormatException e) {
System.out.println(&quot;Invalid entry. Pleaase try again.&quot;);
valid = false;
}
} while (!valid);
morePeople = CAPACITY - people;
if (people &lt; CAPACITY)
System.out.println(&quot;You can invite &quot; + morePeople + &quot; more people to the meeting.&quot;);
else if (people &gt; CAPACITY) {
fewerPeople = people - CAPACITY;
System.out.println(&quot;Sorry, the room is not &quot; + &quot;big enough to seat that many people. You have to &quot;
+ &quot;exclude &quot; + fewerPeople + &quot; from the meeting.&quot;);
}
System.out.println();
System.out.println(&quot;Would you like to make another&quot; + &quot; request?(y /n)&quot;);
answer = scan.nextLine();
} while (answer.equalsIgnoreCase(&quot;y&quot;));
}
}

A sample run:

--------Meeting Request System--------
Welcome to the Meeting Request System. May I know your name?
abc
Hello, abc, how many people will be attending the meeting?
x
Invalid entry. Pleaase try again.
Hello, abc, how many people will be attending the meeting?
10.4
Invalid entry. Pleaase try again.
Hello, abc, how many people will be attending the meeting?
4
You can invite 26 more people to the meeting.
Would you like to make another request?(y /n)
y
Hello, abc, how many people will be attending the meeting?
5
You can invite 25 more people to the meeting.
Would you like to make another request?(y /n)
n

Some other important points:

  1. As you can see, a do...while loop is more appropriate instead of while loop in this case.
  2. You should always check for the NumberFormatException whenever you parse a text (e.g. Scanner::nextLine()) to integer.

huangapple
  • 本文由 发表于 2020年4月7日 11:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61072307.html
匿名

发表评论

匿名网友

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

确定