Java程序无法接收输入并且程序过早终止

时间:2019-06-21 18:19:35

标签: java

我不确定这种编码是否没有接收到adjust变量的输入并在完全运行之前被终止

  public class Question {
  int id;
  String name;
  String type;
  double amt;
  public Question(int id, String name, String type, double amt) {
    this.id = id;
    this.name = name;
    this.type = type;
    this.amt = amt;
    }
    public static void main(String[] args)
    {  }
    }


   import java.util.*;
   public class Answer  {
   public static void gettype(Question[] q,String adjust)
   {
     for(int i=0;i<2;i++)
     {
        if(q[i].getType()==adjust)
        {
            System.out.println(q[i].getId());
        }
    }}

    public static void main(String[] args) {
    int id;
    String name,type,adjust;
    double amt;
    Scanner s=new Scanner(System.in);
    Answer a=new Answer();
    System.out.println("enter 2 car inputs");
    Question[] q=new Question[2];
    for(int i=0;i<2;i++)
    {
        id=s.nextInt();
        s.nextLine();
        name=s.nextLine();
        type=s.nextLine();  
        amt=s.nextDouble();
        q[i]= new Question(id,name,type,amt);
    }
    adjust=s.nextLine();
    a.gettype(q,adjust);
    }
     }

在运行代码时,我能够获取汽车对象数组的输入,但是在那之后,我无法获取变量Adjust的值。 因此,在此方面需要帮助。 我只是尝试在构造函数一侧打印对象。 但无法接收将分配给var Adjust的第9个输入

1 个答案:

答案 0 :(得分:0)

我认为这更好。您应该了解我的工作原理。

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

public class Question {
    private int id;
    private String name;
    private String type;
    private double amt;

    Question(int id, String name, String type, double amt) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.amt = amt;
    }

    int getId() {
        return id;
    }

    String getName() {
        return name;
    }

    String getType() {
        return type;
    }

    double getAmt() {
        return amt;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Question{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", type='").append(type).append('\'');
        sb.append(", amt=").append(amt);
        sb.append('}');
        return sb.toString();
    }
}

class Answer {

    public static final int NUM_QUESTIONS = 2;

    public static void main(String[] args) {
        int numQuestions = (args.length > 0) ? Integer.valueOf(args[0]) : NUM_QUESTIONS;
        List<Question> questions  = new ArrayList<>();
        Scanner s = new Scanner(System.in);
        for (int i = 0; i < numQuestions; ++i) {
            System.out.println(String.format("Question %d", i));
            System.out.print("id: ");
            int id = s.nextInt();
            System.out.print("name: ");
            String name = s.next();
            System.out.print("type: ");
            String type = s.next();
            System.out.print("amt: ");
            double amt = s.nextDouble();
            questions.add(new Question(id, name, type, amt));
        }
        System.out.println(questions);
    }
}