【问题标题】:Error in compiling in jgraspjgrasp编译时出错
【发布时间】:2015-04-18 00:57:27
【问题描述】:
import java.util.Scanner;

public class arrayMethSumPer
{
   public static void main(String[] args)
   {
       int x,sum,copysum;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("enter how many integers you want to enter");
       x = keyboard.nextInt();
       int[] plan=new int [x];
       System.out.println("You said you wanted to enter "+x+" integers, you may now proceed with entering them.");


       for(int i=0;i<x;i++){
           plan[i] = keyboard.nextInt();
           sum=plan[i]+plan[i];
           float[] percentage=(plan[i]*100/sum);
           if(i==x-1)
               System.out.println("your first integer is "+percentage[0]+" percent of the sum");
       }
   }
}

我正在尝试编译它,但在行:

float[] percentage=(plan[i]*100/sum);

显示错误

arrayMethSumPer.java:18:错误:不兼容的类型:int 无法转换为 float[]。

我明白这是什么意思,但是这些类型之间的划分不兼容吗?

有人可以帮我正确打印出每个值的百分比是总和吗?

我可以做的另一件事是使用 (float)() 或 (int)() 进行投射

【问题讨论】:

  • 为什么,确切地说,你试图从一个标量的单个乘法和除法中得到一个数组?该错误表明您无法转换为float[],而不是float。注意括号。

标签: java arrays for-loop arraylist


【解决方案1】:

您绝对可以将ints 和floats 相除,但是,您不能将一个单个 数字除以一个数组 数字。您在错误消息中看到的[] 是关键。您正在进行整数除法,但是您说预期的变量是float[]

如果您打算将此值放在数组中的特定索引处,您可以这样做:

percentage[i] = (plan[i] * 100) / sum;

但是,您似乎没有将以前创建的数组放入其中。如果您只想声明一个新变量,请不使用括号,因此它只是常规的float 变量:

float = plan[i] * 100 / sum;

这将编译。如果您确实想将它们存储在数组中,请确保将数组声明为 BEFORE 您的循环如下:

int[] list = new int[x];
double[] percentages = new double[x];

【讨论】:

    【解决方案2】:

    float[] percentage=(plan[i]*100/sum); 语句中,您试图将一个int 值分配给一个float 数组。这是做不到的。 您必须根据您的要求将plan[i]*100/sum 的值分配给intfloat。 例如

    int x = plan[i]*100/sum; // int/int -> int
    

    但我想你想在这里得到小数的值,所以它应该是

    percentage[/*desired index*/] = (float) (plan[i] * 100) / sum;
    

    除了这些之外,在你的 for 循环中,你在每次迭代中创建数组。那真的很糟糕。我认为你应该有类似的东西

    float[] percentage= new float[x];
    for(int i=0;i<x;i++){
        plan[i] = keyboard.nextInt();
        sum = plan[i]+plan[i];
        percentage[x] = (float)(plan[i]*100/sum);
        if(i == x-1)
             System.out.println("your first integer is "+percentage[0]+" percent of the sum");
    }
    

    【讨论】:

      【解决方案3】:

      这是如何打印每个值占总和的百分比。

      import java.util.Scanner;
      public class PercentCalc{
      
        public static void main(String[] args){
      
          Scanner keyboard = new Scanner(System.in);
          System.out.print("How many numbers would you like to enter? :\n");
          int x = keyboard.nextInt();
          int[] list = new int[x];
          double [] percentValue = new double[x];
      
          int sum = 0;
      
          for(int i =0; i<list.length; i++){
            int num = i+1;
            System.out.print("Please enter #" + num + ": ");
            list[i] = keyboard.nextInt();
            sum = list[i] + sum;
          }
      
          for(int i =0; i<list.length; i++){
            percentValue[i] = ((double)list[i] / sum)*100.0;
            System.out.printf("%d is %1.2f percent of the sum.\n", list[i], percentValue[i]);
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-30
        • 1970-01-01
        • 2012-01-28
        • 2013-08-16
        • 2017-07-13
        相关资源
        最近更新 更多