简体   繁体   English

如何取消Java中的for循环?

[英]How do I cancel a for loop in java?

The goal of my program is to store a default of 100 Employee objects(but should work with less) to an array after prompting the user input of each employee's name, ssn, and salary with a loop.I then need to output the contents of the array. 我的程序的目标是在循环提示用户输入每个员工的姓名,ssn和薪水之后,将默认的100个Employee对象(但应该使用更少的对象)存储到数组中,然后我需要输出数组。 I am trying to end the loop when the user inputs a 0 when asked for a name, but so far my program seems to loop endlessly. 我试图在用户输入名称时输入0时结束循环,但是到目前为止,我的程序似乎无休止地循环。

Driver class: 驱动类别:

import java.util.Scanner;

public class EmployeeDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner kb = new Scanner(System.in);

        Employee employees[]= new Employee[100];
        String nameTest="";
        for(int i=0;i<100;i++){
            Employee e = new Employee();

        System.out.println("Enter Employee name, Ssn, and salary when prompted");
        System.out.println("When finished type in 0's for each prompt");
        System.out.println("Enter Name: ");
        nameTest=kb.next();
        e.setName(nameTest);
        System.out.println("Enter Ssn: ");
        e.setSsn(kb.next());
        System.out.println("Enter Salary: ");
        e.setSalary(kb.nextDouble());

        if(nameTest=="0"){
            i=100;
        }
        else
            employees[i]=e;
        }

        for(int x=0;x<100;x++){
        employees[x].toString();
        }
    }

}

Methods: 方法:

public class Employee extends Person {

    String ssn;
    double salary;
    public String getSsn() {
        return ssn;
    }
    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Employee(String initialName,String initialSsn,double initialSalary) {
        super(initialName);
        ssn=initialSsn;
        salary=initialSalary;
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Name: "+getName()+" Ssn: "+getSsn()+" Salary: "+getSalary();
    }
}

You should be using equals() to compare strings rather than == , as in the following line: 您应该使用equals()来比较字符串而不是== ,如以下行所示:

if (nameTest == "0")

In order to acheive what you want, you'd use something along the following lines: 为了达到您想要的目的,可以按照以下方式使用:

System.out.println("Enter Name: ");
nameTest = kb.next();
if (nameTest.equals("0")) break;      // <- add this line here.

This has the advantage of only requiring you to enter 0 for the employee name rather than all three values having to be entered. 这样的好处是只要求您输入0作为员工姓名,而不必输入所有三个值。

Please follow as below 请按照以下步骤

== will test for reference equality only. ==将仅测试引用相等性。

.equals() tests for value if equal. .equals()测试是否相等。

So replace if (nameTest == "0") with if (nameTest.equals("0")) 因此,将if (nameTest == "0")替换为if (nameTest.equals("0"))

First, you are checking an int against a String , which will not work. 首先,您要根据String检查一个int ,它将不起作用。 You should change that to if (nameTest.equals("0") ) . 您应该将其更改为if (nameTest.equals("0") ) Then you can use break to break out of the loop. 然后,您可以使用break打破循环。 No need to make i=100 . 无需使i=100

There is some problem in this code snippet: 此代码段中存在一些问题:

if(nameTest=="0"){
    i=100;
}

You should always use "equals" method instead of "==" when judge the value of String to be equal. 当判断String的值相等时,应始终使用“等于”方法而不是“ ==”。 Accordingly, write the code like this: 因此,编写如下代码:

if (nameTest.equals("0")) {
    i = 100;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM