有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

尝试用java打印数组

我需要提交一个代码,每次尝试运行它时,都会一次又一次地出现相同的错误

问题是

Write the following Java methods:

(a). readValues to input ten integer values into an array of integers TAB from a text file “Values.txt”. This array TAB is passed to the method as parameter. Assume that the number of students in the file is equal to the length of the array.

(b). oddValues that takes the array TAB as parameter and returns the number of odd values found in TAB.

(c). replaceOdd that takes the array TAB as a parameter. It should replace every odd value in TAB by the sum of all odd values.

Hint: your method must first compute the sum of all odd values.

(d). printValues that takes the array TAB as a parameter and prints its content on the screen.

(e). main that declares the array TAB and calls the above four methods.

N.B.: In your program, use the methods and variable names as mentioned above.

下面是代码:

import java.util.*;
import java.io.*;

public class Finalexam
{
    public static void main (String [] args ) throws FileNotFoundException 
{
    int sum=0;
    int [] TAB=new int [10];
    ReadValues(TAB);
    oddValues(TAB);
    replaceOdd(TAB);
    printValues(TAB);

    System.out.println("The sum is" + sum);

}
public static void ReadValues (int [] TAB)
{
 {   int i;
    for(i=0; i<10; i++){
     Scanner s = new Scanner ("Values.txt") ;
     TAB[i]=s.nextInt();
    }
}
    s.close();
}
public static double oddValues(int[] TAB)
{
    int i;
    double odd=0;
    int fn=0;
    for(i=1; i<odd; i++){

    
    while(odd % 2 !=0)
        {
            odd = fn;
        }
        break;
    }
    return fn;
}

public static int replaceOdd(int[] TAB)
{
    int re=0;
    for(int i=0; i<TAB.length; i++){
        re = re/TAB.length;
    }
    return re;
}
public static void printValues(int[] TAB)
{
    int i;
    for(i=0; i<10; i++){
        System.out.println(TAB[i]+"\t");
    }
    System.out.println();
}
}

哪一部分我做错了?我甚至不能运行它


共 (1) 个答案

  1. # 1 楼答案

    首先,代码中有一个编译错误

    用你的方法

    public static void ReadValues (int [] TAB)
        {
            {   int i;
                for(i=0; i<10; i++){
                    Scanner s = new Scanner ("Values.txt") ;
                    TAB[i]=s.nextInt();
                }
            }
            s.close();
        }
    

    你有太多额外的括号,但这不是问题所在,问题是扫描对象s在for循环内声明,当你稍后在循环外关闭它时,因为变量的作用域不在循环外,因此出现了错误
    正确的方法应该是

    public static void readValues (int [] tab){
            int i;
            Scanner s = new Scanner ("Values.txt") ;
            for(i=0; i<10; i++){
                    tab[i]=s.nextInt();
            }
            s.close();
    }
    

    还有很多东西可以在你的代码中使用,但这是一种不好的做法,或者没有遵循惯例

    1. 变量名(如tab)应始终为驼峰大小写。只有当它是一个常数时,它才应该是一个大写,而你的情况不是这样
    2. 方法名称以小写字母开头
    3. 此外,您正在调用两个方法replaceOdd(TAB)oddValues(TAB),但返回值没有在任何地方使用
    4. 永远不会抛出FileNotFoundException

    如果你仔细看看下面这个方法

    public static double oddValues(int[] TAB) {
        int i;
        double odd = 0;
        int fn = 0;
        for (i = 1; i < odd; i++) {
    
            while (odd % 2 != 0) {
                odd = fn;
            }
            break;
        }
        return fn;
    }
    

    循环永远不会执行,因为odd0,所以i<;奇数总是假的。奇数的逻辑也是错误的

    public static int replaceOdd(int[] TAB){
        int re=0;
        for(int i=0; i<TAB.length; i++){
            re = re/TAB.length;
        }
        return re;
    }
    

    这种方法总是返回零,逻辑是错误的

    还有更多的逻辑错误。我建议你也调查一下