Java for循环中的多个数组

Java for循环中的多个数组,java,Java,我一直在编写一小部分代码,帮助我计算一部电影周末的票房表现。我希望它需要 电影名称 使用它要花多少钱 预计那个周末能赚多少钱 然后我想让它输出上面所有的信息,加上每部电影的表现。我在保存多个String[]值以及int[]值时遇到了问题。我可以删除字符串行,所有int[]行(包括价格和预测)都可以正常工作,或者我可以删除int[]行和字符串[]也可以正常工作。有人能帮我找出错误吗 import java.util.*; public class FMLArrays { public st

我一直在编写一小部分代码,帮助我计算一部电影周末的票房表现。我希望它需要

  • 电影名称
  • 使用它要花多少钱
  • 预计那个周末能赚多少钱
  • 然后我想让它输出上面所有的信息,加上每部电影的表现。我在保存多个
    String[]
    值以及
    int[]
    值时遇到了问题。我可以删除
    字符串
    行,所有
    int[]
    行(包括价格和预测)都可以正常工作,或者我可以删除
    int[]
    行和
    字符串[]
    也可以正常工作。有人能帮我找出错误吗

    import java.util.*;
    public class FMLArrays {
    
       public static void main(String[] args) {
    
          Scanner console = new Scanner(System.in);
    
          String[] movieName = new String[15];
          int[] moviePrice = new int[15];
          int[] moviePrediction = new int[15];
          int[] moviePreformance = new int[15];
    
          for (int i = 0; i < movieName.length; i++) {         
    
             System.out.print("Enter the name of movie " + (i+1) + ": ");
             movieName[i] = console.nextLine();
    
             System.out.print("Enter the price of movie " + (i+1) + ": ");
             moviePrice[i] = console.nextInt();
    
             System.out.print("Enetr the prediction of movie " + (i+1) + ": ");
             moviePrediction[i] = console.nextInt();
    
             moviePreformance[i] = Math.round(((moviePrediction[i] * 1000000) / moviePrice[i]) * 100) / 100;
    
             System.out.println();
    
          }
    
          for (int i = 0; i < movieName.length; i++) {
    
             System.out.println();
             System.out.println("Name: " + movieName[i]);
             System.out.println("Price: $" + moviePrice[i]);
             System.out.println("Prediction: $" + moviePrediction[i] + " million");
             System.out.println("Preformance: $" + moviePreformance[i]);
    
          }
    
       }
    
    }
    
    import java.util.*;
    公共类射线{
    公共静态void main(字符串[]args){
    扫描仪控制台=新扫描仪(System.in);
    String[]movieName=新字符串[15];
    int[]moviePrice=新int[15];
    int[]moviePrediction=新int[15];
    int[]电影性能=新int[15];
    对于(int i=0;i
    这应该可以解决问题:

    moviePrice[i] = console.nextInt();
    console.nextLine();
    
    moviePrediction[i] = console.nextInt();
    console.nextLine(); 
    
    console.nextInt()只读取数字,而不读取行的其余部分,如回车。

    当您使用nextInt()并输入一个数字时,您还输入了一个新行字符,因此下次调用nextLine()时,这个新行字符是您等待的下一个输入。 您可以通过始终使用nextLine或添加对nextLine的调用来解决此问题

    import java.util.*;
    public class FMLArrays {
    
        public static void main(String[] args) {
    
            Scanner console = new Scanner(System.in);
    
            String[] movieName = new String[2];
            int[] moviePrice = new int[2];
            int[] moviePrediction = new int[2];
            int[] moviePreformance = new int[2];
    
            for (int i = 0; i < movieName.length; i++) {
    
                System.out.print("Enter the name of movie " + (i+1) + ": ");
                movieName[i] = console.nextLine();
    
                System.out.print("Enter the price of movie " + (i+1) + ": ");
                moviePrice[i] = Integer.parseInt(console.nextLine());
    
                System.out.print("Enetr the prediction of movie " + (i+1) + ": ");
                moviePrediction[i] = Integer.parseInt(console.nextLine());
    
                moviePreformance[i] = Math.round(((moviePrediction[i] * 1000000) / moviePrice[i]) * 100) / 100;
    
                System.out.println();
    
            }
    
            for (int i = 0; i < movieName.length; i++) {
    
                System.out.println();
                System.out.println("Name: " + movieName[i]);
                System.out.println("Price: $" + moviePrice[i]);
                System.out.println("Prediction: $" + moviePrediction[i] + " million");
                System.out.println("Preformance: $" + moviePreformance[i]);
    
            }
    
        }
    
    }
    
    import java.util.*;
    公共类射线{
    公共静态void main(字符串[]args){
    扫描仪控制台=新扫描仪(System.in);
    String[]movieName=新字符串[2];
    int[]moviePrice=新int[2];
    int[]moviePrediction=新int[2];
    int[]电影性能=新int[2];
    for(int i=0;i
    您不应该使用多个数组,而应该创建一个
    电影
    类来保存此信息。--“有人能帮我找到我的错误吗?”-哪些错误?什么不起作用?为什么它不工作(编译器错误?异常?不需要的行为?)?请准确一点。我研究了为什么会发生这种情况,并决定在字符串[]之后使用while语句进行搜索,直到输出大于0为止。我不想让每个int[]成为下一行,因为我不想让用户输入数字以外的任何东西。