Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我该如何解决这个问题;浮动不能转换为浮动[]”;下面的代码中有错误吗?_Java_Arrays_User Input - Fatal编程技术网

Java 我该如何解决这个问题;浮动不能转换为浮动[]”;下面的代码中有错误吗?

Java 我该如何解决这个问题;浮动不能转换为浮动[]”;下面的代码中有错误吗?,java,arrays,user-input,Java,Arrays,User Input,我写了一个小程序,从用户那里获取元素的数量和这些元素的价格,然后打印价格 但是这段代码的第12行给出了错误“float不能转换为float[]”,我不知道如何解决这个问题。如果需要,请提供一些帮助或修改代码 import java.util.Scanner; public class Main{ public static void main( String args[]){ System.out.println("enter the number of

我写了一个小程序,从用户那里获取元素的数量和这些元素的价格,然后打印价格

但是这段代码的第12行给出了错误“float不能转换为float[]”,我不知道如何解决这个问题。如果需要,请提供一些帮助或修改代码

     import java.util.Scanner;
     public class Main{
     public static void main( String args[]){
     System.out.println("enter the number of elements :" );
     Scanner s1= new Scanner(System.in);
     int N = s1.nextInt();
     System.out.println("enter the price of all the elements in ascending
     order: ");
     float[] price =new float[N];

    for(int i=0; i<N;i++){
    price=s1.nextFloat();
    System.out.println(price);
    }
}
    }
import java.util.Scanner;
公共班机{
公共静态void main(字符串参数[]){
System.out.println(“输入元素数:”);
扫描仪s1=新的扫描仪(System.in);
int N=s1.nextInt();
System.out.println(“以升序输入所有元素的价格
命令:“);
浮动[]价格=新浮动[N];

for(int i=0;i当您编写代码时,格式和命名是必须的,因为它使您的代码更可读、更容易理解。我已对您的代码进行了格式设置,并更新了适当的输入

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        System.out.println("enter the number of elements :");
        Scanner s1 = null;
        try {
            s1 = new Scanner(System.in);
            int totalElements = s1.nextInt();
            System.out.println("enter the price of all the elements in ascending order :");
            float[] price = new float[totalElements];
            for (int i = 0; i < totalElements; i++) {
                price[i] = s1.nextFloat();
                System.out.println(Arrays.toString(price));
            }
        } finally {
            if (s1 != null) {
                s1.close();
            }
        }

    }
}
导入java.util.array;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串参数[]){
System.out.println(“输入元素数:”);
扫描程序s1=空;
试一试{
s1=新扫描仪(系统英寸);
int totalElements=s1.nextInt();
System.out.println(“按升序输入所有元素的价格:”);
浮动[]价格=新浮动[总要素];
对于(int i=0;i
请注意以下事项:

  • 变量名应以小写开头,并遵循驼峰式大小写

  • 无论何时使用扫描仪,都需要关闭

  • 通过将float分配给数组中的一个位置而不是数组,可以解决您的错误。例如,price[i]=s1.nextFloat()

  • 要打印数组,请使用Arrays.toString()函数

  • 快乐编码

    导入java.util.Scanner;
    
    import java.util.Scanner;
    
    public class Main{
    
      public static void main( String args[]){
    
        System.out.println("enter the number of elements :" );
         Scanner s1= new Scanner(System.in);
          int N = s1.nextInt();
            System.out.println("enter the price of all the elements in ascending 
        order :");
    
             float[] price =new float[N];
    
           for(int i=0; i<N;i++){
    
              price[i]=s1.nextFloat();
              System.out.println(price[i]);
               }
    
               }
    }
    
    公共班机{ 公共静态void main(字符串参数[]){ System.out.println(“输入元素数:”); 扫描仪s1=新的扫描仪(System.in); int N=s1.nextInt(); System.out.println(“以升序输入所有元素的价格 命令:“); 浮动[]价格=新浮动[N];
    对于代码中的(inti=0;i,
    price
    是一个数组

    类型为T的数组由元素组成,每个元素都是类型为T的。因此,您需要将数组中的一个元素分配给浮点

       price[i] = myFloat;
    

    请正确格式化您的代码。您必须将值放入price中的
    i
    th位置。并且将值放入
    i
    th位置:
    array[i]=..
    array
    是数组变量,
    ..
    表示必须进入数组中该位置的内容)只需执行
    price[i]=s1.nextFloat()
    请记住,
    price
    是一个
    浮点数组
    ,而不是
    float
    本身。你必须分别访问它的每个值。代码中的缩进非常糟糕。请修复它。你可以(不像论坛)。提前谢谢。代码中的缩进非常糟糕。请修复它。提前谢谢。
       price[i] = myFloat;