Java:循环不起作用,变量初始化错误

时间:2015-10-09 10:59:32

标签: java loops variables initialization

如果用户输入“是”以及为什么变量初始化存在错误,请解释为什么此循环不起作用。

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

public class Main { 

  public static void main(String args[]) {
    Scanner src;
    String amount;
    String counterparty;
    String dt;
    String ct;
    System.out.println("Create new transaction:yes/no");
    Scanner abc = new Scanner(System.in);
    String g = abc.nextLine();

    if (g=="yes") {
      System.out.println("Amount of transaction:");
      src = new Scanner(System.in);
      amount = src.nextLine();
      System.out.println("Counterparty:");
      counterparty = src.nextLine();
      System.out.println("Dt:");
      dt = src.nextLine();
      System.out.println("Ct:");
      ct = src.nextLine();
    }
    else if (g=="no") {
      amount="0";
    }

    System.out.println("Transaction:");
    ArrayList <String> Provodka = new ArrayList();
    Provodka.add(amount);
    Provodka.add(counterparty);
    Provodka.add(dt);
    Provodka.add(ct);
    for (int i = 0; i < Provodka.size(); i++) {
        String value = Provodka.get(i);
        System.out.println("Element: " + value);
    }
  }
}

3 个答案:

答案 0 :(得分:1)

只需初始化局部变量并使用equals()方法而不是“==”

public static void main(String args[]) {
    Scanner src;
    String amount = null;
    String counterparty = null;
    String dt = null;
    String ct = null;
    System.out.println("Create new transaction:yes/no");
    Scanner abc = new Scanner(System.in);
    String g = abc.nextLine();
           if (g.equals("yes"))
                   {
        System.out.println("Amount of transaction:");
         src = new Scanner(System.in);
        amount = src.nextLine();
        System.out.println("Counterparty:");
         counterparty = src.nextLine();
        System.out.println("Dt:");
         dt = src.nextLine();
        System.out.println("Ct:");
         ct = src.nextLine();
    }
  else if (g.equals("no")){
   amount="0";
    }

    System.out.println("Transaction:");
    ArrayList <String> Provodka = new ArrayList();
    Provodka.add(amount);
    Provodka.add(counterparty);
    Provodka.add(dt);
    Provodka.add(ct);
    for (int i = 0; i < Provodka.size(); i++) {
        String value = Provodka.get(i);
        System.out.println("Element: " + value);
    }
}

答案 1 :(得分:0)

在字符串比较中,您将字符串与'=='进行比较。 使用equals()方法比较字符串。 例如: -

if ("yes".equals(g)){

}

答案 2 :(得分:0)

首先,Scanner有很多不必要的声明。将一个变量用于扫描仪将适用于所有输入。其次,将变量声明在main方法之上并使它们变为静态,在这里您不必总是初始化它们。最后,使用g.equalsIgnoreCase(“yes”)而不是g ==“yes”,这样如果你在CAPS中键入yes,它仍然会注册。尝试下面的操作

    public static String g; 
    public static String amount;
    public static String counterparty;
    public static String dt;
    public static String ct;

    public static void main(String args[]) {

          Scanner s = new Scanner(System.in);


          System.out.println("Create new transaction:yes/no");
              g= s.nextLine();

          if (g.equalsIgnoreCase("yes")) {
              System.out.println("Amount of transaction: ");
                  amount = s.nextLine();

              System.out.println("Counterparty: ");
                  counterparty = s.nextLine();

              System.out.println("Dt: ");
                  dt = s.nextLine();

              System.out.println("Ct: ");
                  ct = s.nextLine();
          }
          else if (g.equalsIgnoreCase("no")) {

              amount = "0";
          }

          System.out.println("Transaction:");
          ArrayList <String> Provodka = new ArrayList();
          Provodka.add(amount);
          Provodka.add(counterparty);
          Provodka.add(dt);
          Provodka.add(ct);

          for (int i = 0; i < Provodka.size(); i++) {
              String value = Provodka.get(i);
              System.out.println("Element: " + value);
          }
      }
  }