【问题标题】:Statement in a for loop executing only oncefor循环中的语句只执行一次
【发布时间】:2014-06-07 20:24:05
【问题描述】:
import java.util.Scanner;

public class Test {

    public static void main(String args[]) {
        String name = " ", n;
        double r, h, s, m = 0;
        int c;

        Scanner i = new Scanner(System.in);
        Employee e[] = new Employee[5];

        for (int j = 0; j < 5; j++) {
            System.out.printf("\nPlease enter the name of Employee %d ", j + 1);
            /* THE PROBLEM LIES IN THE ABOVE STATEMENT IT IS EXECUTED ONLY ONCE WHILE IT'S MEANT TO BE EXECUTED 5 TIMES */
            n = i.nextLine();
            System.out.print("\t1.PartTime\n\t2.FullTime\n");
            c = i.nextInt();

            switch (c) {
                case 1:
                    System.out.print("hours worked ");
                    h = i.nextDouble();
                    System.out.print("rate per hour ");
                    r = i.nextDouble();
                    e[j] = new Part(n, h, r);
                    break;
                case 2:
                    System.out.print("Salary ");
                    s = i.nextDouble();
                    e[j] = new Full(n, s);
                    break;
                default:
                    System.out.print("\n***INVALID*** ");
                    j--;
            }
        }
    }
}

【问题讨论】:

  • 只是好奇:您希望通过将句子全部大写来实现什么?
  • System.out 默认刷新换行符。您的许多打印件可能没有冲洗。
  • @Braj 的休息是为 switch 语句
  • 它按预期工作。分享实际代码。
  • Swing 是如何参与其中的?

标签: java loops for-loop java.util.scanner


【解决方案1】:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

所以你应该在每个 case 之后添加一个中断。

我更喜欢使用 println 而不是 printf,它可以让你使用更少的“\n”,让你的代码更有条理。

您使用 java 就像使用 C++ 一样,我认为这样做要容易得多 System.out.println("请输入员工姓名" + (j+1));

如果没有其余的源代码,帮助您有点困难。

【讨论】:

    【解决方案2】:

    问题是,在第二个循环中,n = i.nextLine(); 被跳过,因为Scanners 缓冲区中仍有内容行,您需要清除 Scanner

    你可以添加类似...

    if (i.hasNextLine()) {
        i.nextLine();
    }
    

    for-loop结尾,例如..

    for (int j = 0; j < 5; j++) {
        System.out.printf("\nPlease enter the name of Employee %d ", j + 1);
        /* THE PROBLEM LIES IN THE ABOVE STATEMENT IT IS EXECUTED ONLY ONCE WHILE IT'S MEANT TO BE EXECUTED 5 TIMES */
        n = i.nextLine();
        System.out.print("\t1.PartTime\n\t2.FullTime\n");
        c = i.nextInt();
    
        switch (c) {
            case 1:
                System.out.print("hours worked ");
                h = i.nextDouble();
                System.out.print("rate per hour ");
                r = i.nextDouble();
                e[j] = new Part(n, h, r);
                break;
            case 2:
                System.out.print("Salary ");
                s = i.nextDouble();
                e[j] = new Full(n, s);
                break;
            default:
                System.out.print("\n***INVALID*** ");
                j--;
        }
        if (i.hasNextLine()) {
            i.nextLine();
        }
    }
    

    这将确保Scanner 没有来自先前输入的其他内容

    【讨论】:

      最近更新 更多