不知道发生了什么 - 没有错误(Java新手)

时间:2016-11-17 22:42:12

标签: java if-statement

只要你教我,我就准备好学习。我只是在摆弄Java,因为它是我学习的最佳方式。如果您发现任何您认为我可以在我的代码中改进的内容,我很乐意听到它。

我为自己准备的项目的总体目标是使用Java创建一个存储人员信息的程序。然后将该信息传输到Tomcat服务器,我将设置运行SQL。

现在您已了解我的总体目标(我远离它),我的问题如下:

我可以找到代码的一部分,询问我输入命令和名称等没问题。但是,当我输入"读"然后按回车,它似乎跳过了我已设置的声明并直接进入其他部分。

修改:我更新了帖子以显示解决了问题的原因。

工作代码:

import javax.print.DocFlavor.READER;

import java.util.ArrayList;
import java.util.Scanner;


public class Main {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int total = 0;
    int years = 0;
    int invested =0;
    ArrayList<String> names = new ArrayList<String>(0);
    ArrayList<String> address = new ArrayList<String>(0);
    ArrayList<String> ids = new ArrayList<String>(0);
    Scanner sReader = new Scanner(System.in);

    while(reader.nextLine().toLowerCase().split(" ")[0] != "exit")
    {
    System.out.print("Enter Instruction: ");
    String n = reader.nextLine();
    String[] uin = n.toLowerCase().split(" ");
    if(uin[0].equals("add"))
    {
            try {
                System.out.println(add(Integer.parseInt(uin[1]),Integer.parseInt(uin[2])));
                } catch (NumberFormatException e) {
                System.out.println("Numbers Only");
                System.out.print(e.getMessage());
                } catch (Exception e){
                    System.out.println("ERROR");
                }
    }


    if(uin[0].equals("sub") || uin[0].equals("subtract"))
    {
            try {
                System.out.print(Integer.parseInt(uin[1])-Integer.parseInt(uin[2]));
                } catch (NumberFormatException e){
                System.out.println("Numbers Only");
                System.out.print(e.getMessage());
                } catch (Exception e){
                    System.out.println("ERROR");
                }

    }

    if(uin[0].equals("string"))
    {
        String string = "this is a string.";
        System.out.println(string.indexOf("cat"));
    }
    if(uin[0].equals("a"))
    {
        total+=Integer.parseInt(uin[1]);
        invested+=Integer.parseInt(uin[1]);
    }
    if(uin[0].equals("aa"))
    {
        total+=10000;
        invested+=10000;
    }
    if(uin[0].equals("bb"))
    {
        total-=55000;
        invested-=55000;
    }
    if(uin[0].equals("aq"))
    {
        for(int i = 0;i < Integer.parseInt(uin[1]);i++){
            total+=10000;
            invested+=10000;
            total*=1.06;
            years+=1;
        }
        years-=1;
    }
    if(uin[0].equals("exit"))
    {
        break;
    }
    if(uin[0].equals("entry")){
        for(int i=0;i==0;i+=0){
            System.out.print("Entry Command: ");
            String input = sReader.nextLine();
            if(input.equals("exit")){
                i = 1;
            }else if(input.equals("read")){
                for(int y=0;y<names.size();y++){
                    System.out.println("User #"+y);
                    System.out.println(names.get(y).toString());
                    System.out.println(address.get(y).toString());
                    System.out.println(ids.get(y).toString());
                }
            }else{
                System.out.print("Name: ");
                input = sReader.nextLine();
                names.add(input);
                System.out.print("Address: ");
                input = sReader.nextLine();
                address.add(input);
                System.out.print("ID: ");
                input = sReader.nextLine();
                ids.add(input);
            }
        }
    }
    total*=1.06;
    years+=1;
    double earn = Math.floor(total) * 0.06;
    System.out.println("Bank: " + total);
    System.out.println("Invested: " + invested);
    System.out.println("Yearly Earnings: " + earn);
    System.out.println("Years: " + years);
    }
}
public static int add(int n1, int n2){
int out;
try{
    out = (n1+n2);
    return(out);
}catch (Exception e){
    System.out.print("ERROR");
    return(0);
}
}
}

旧代码:

import javax.print.DocFlavor.READER;

import java.util.ArrayList;
import java.util.Scanner;


public class Main {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int total = 0;
    int years = 0;
    int invested =0;
    ArrayList<String> names = new ArrayList<String>(0);
    ArrayList<String> address = new ArrayList<String>(0);
    ArrayList<String> ids = new ArrayList<String>(0);
    Scanner sReader = new Scanner(System.in);

    while(reader.nextLine().toLowerCase().split(" ")[0] != "exit")
    {
    System.out.print("Enter Instruction: ");
    String n = reader.nextLine();
    String[] uin = n.toLowerCase().split(" ");
    if(uin[0].equals("add"))
    {
            try {
                System.out.println(add(Integer.parseInt(uin[1]),Integer.parseInt(uin[2])));
                } catch (NumberFormatException e) {
                System.out.println("Numbers Only");
                System.out.print(e.getMessage());
                } catch (Exception e){
                    System.out.println("ERROR");
                }
    }


    if(uin[0].equals("sub") || uin[0].equals("subtract"))
    {
            try {
                System.out.print(Integer.parseInt(uin[1])-Integer.parseInt(uin[2]));
                } catch (NumberFormatException e){
                System.out.println("Numbers Only");
                System.out.print(e.getMessage());
                } catch (Exception e){
                    System.out.println("ERROR");
                }

    }

    if(uin[0].equals("string"))
    {
        String string = "this is a string.";
        System.out.println(string.indexOf("cat"));
    }
    if(uin[0].equals("a"))
    {
        total+=Integer.parseInt(uin[1]);
        invested+=Integer.parseInt(uin[1]);
    }
    if(uin[0].equals("aa"))
    {
        total+=10000;
        invested+=10000;
    }
    if(uin[0].equals("bb"))
    {
        total-=55000;
        invested-=55000;
    }
    if(uin[0].equals("aq"))
    {
        for(int i = 0;i < Integer.parseInt(uin[1]);i++){
            total+=10000;
            invested+=10000;
            total*=1.06;
            years+=1;
        }
        years-=1;
    }
    if(uin[0].equals("exit"))
    {
        break;
    }
    if(uin[0].equals("entry")){
        for(int i=0;i==0;i+=0){
            System.out.print("Entry Command: ");
            sReader = new Scanner(System.in);
            System.out.println(sReader.nextLine());
            if(sReader.nextLine().equals("exit")){
                i = 1;
            }else if(sReader.nextLine().equals("read")){
                System.out.println(names.size());
                System.out.println("yup");
                for(int y=0;y==names.size()-1;y++){
                    System.out.println(names);
                    System.out.println(names.get(y));
                    System.out.println(address.get(y));
                    System.out.println(ids.get(y));
                }
            }else{
                System.out.print("Name: ");
                sReader = new Scanner(System.in);
                names.add(sReader.nextLine());
                System.out.print("Address: ");
                sReader = new Scanner(System.in);
                address.add(sReader.nextLine());
                System.out.print("ID: ");
                sReader = new Scanner(System.in);
                ids.add(sReader.nextLine());
            }
        }
    }
    total*=1.06;
    years+=1;
    double earn = Math.floor(total) * 0.06;
    System.out.println("Bank: " + total);
    System.out.println("Invested: " + invested);
    System.out.println("Yearly Earnings: " + earn);
    System.out.println("Years: " + years);
    }
}
public static int add(int n1, int n2){
int out;
try{
    out = (n1+n2);
    return(out);
}catch (Exception e){
    System.out.print("ERROR");
    return(0);
}
}
}

1 个答案:

答案 0 :(得分:4)

这是错误的,因为你正在做System.out.println(sReader.nextLine()) - 这会自动获取最后一个输入(在你的例子中为"exit")。但是当你尝试if (sReader.nextLine().equals("exit"))时,它会再次寻找下一行。

要修复,您可以将输入存储在变量中:

String input = sReader.nextLine();
System.out.println(input);
if (input.equals("exit")) {
    // etc
}