Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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_Compiler Errors - Fatal编程技术网

整理代码和修复错误(java)

整理代码和修复错误(java),java,compiler-errors,Java,Compiler Errors,我需要修改代码以便 它的结构更好 它更具可读性 它使用方法和参数 如果要输入不同数量的整数,则只需更改一条语句 我的新代码在底部,但它还没有工作,我不太明白我还需要做什么。如果有人能帮忙,那就太好了 原始代码 import java.util.Scanner; 公共课让我变得更好 { 公共静态void main(字符串[]args) { int[]a={0,0,0,0,0,0,0,0,0,0}; 扫描仪b=新的扫描仪(System.in); System.out.println(“请在1-1

我需要修改代码以便

  • 它的结构更好
  • 它更具可读性
  • 它使用方法和参数
  • 如果要输入不同数量的整数,则只需更改一条语句
我的新代码在底部,但它还没有工作,我不太明白我还需要做什么。如果有人能帮忙,那就太好了

原始代码
import java.util.Scanner;
公共课让我变得更好
{
公共静态void main(字符串[]args)
{
int[]a={0,0,0,0,0,0,0,0,0,0};
扫描仪b=新的扫描仪(System.in);
System.out.println(“请在1-19:>”范围内输入10个数字”;
int c=b.nextInt();
而(c<1 | | c>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[0]=c;
int d=b.nextInt();
而(d<1 | | d>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[1]=d;
int e=b.nextInt();
而(e<1 | | e>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[2]=e;
int f=b.nextInt();
而(f<1 | | f>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[3]=f;
int g=b.nextInt();
而(g<1 | | g>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[4]=g;
int h=b.nextInt();
而(h<1 | | h>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[5]=h;
int i=b.nextInt();
而(i<1 | | i>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[6]=i;
int j=b.nextInt();
而(j<1 | | j>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[7]=j;
int k=b.nextInt();
而(k<1 | | k>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[8]=k;
int l=b.nextInt();
而(l<1 | | l>19)
{
System.out.println(“不在1-19范围内,请重试:>”;
}
a[9]=l;
System.out.println(“\n数组内容”);
系统输出打印((a[0]<10?”:“”)+a[0]+“”);
系统输出打印((a[1]<10?”:“”)+a[1]+“”);
系统输出打印((a[2]<10?”:“”)+a[2]+“”);
系统输出打印((a[3]<10?”:“”)+a[3]+“”);
系统输出打印((a[4]<10?”:“”)+a[4]+“”);
系统输出打印((a[5]<10?”:“”)+a[5]+“”);
系统输出打印((a[6]<10?”:“”)+a[6]+“”);
系统输出打印((a[7]<10?”:“”)+a[7]+“”);
系统输出打印((a[8]<10?”:“”)+a[8]+“”);
系统输出打印((a[9]<10?”:“”)+a[9]+“”);
System.out.println();
布尔值noSwap=false;
int startAt=0;
int stopAt=9;
while(startAt
新代码
import java.util.Scanner;
导入java.math.*;
公共类使我变得更好{
公共静态void main(字符串[]args){
扫描仪kybd=新扫描仪(System.in);
整数[]个数=新整数[10];
int数组=0;
System.out.println(“输入10个整数:”);
for(int i=0;i
考虑使用比基元数组更动态的结构

我喜欢ArrayList:

将长代码重新组织为
while
for
循环

例如:

ArrayList<Integer> a = new ArrayList<Integer>();
Scanner b = new Scanner(System.in);

Integer c = null;
while (a.size() != 10) {
    System.out.println("Please input a number in the range 1-19 :> ");
    c = b.nextInt();
    while (c < 1 || c > 19)
    {
        System.out.println("Not in the range 1-19, please try again :> ");
        c = b.nextInt();
    }
    a.add(c);
}

System.out.println("\nArray contents");
for (int i=0; i<10; i++) {
    System.out.print((a.get(i) < 10 ? " " : "") + a.get(i) + "  ");
}
ArrayList a=新的ArrayList();
扫描仪b=新的扫描仪(System.in);
整数c=null;
而(a.大小()!=10){
System.out.println(“请输入1-19:>”范围内的数字);
c=b.nextInt();
而(c<1 | | c>19)
{
System.out.println(“不在
import java.util.Scanner;
import java.math.*;
public class MakeMeBetterImproved {

    public static void main(String[] args) {
        Scanner kybd = new Scanner(System.in);
        int[] numbers = new int[10];
        int array = 0;

        System.out.println("Enter 10 integers: ");
        for (int i = 0; i < numbers.length; i++)  {
            numbers[i] = kybd.nextInt();
            int MyIntArray = array + i;

        }            

        System.out.println("The minimum number is: " + math.min);
        System.out.println("The maximum number is: " + math.max);
        System.out.println("The average value is: " + math.average);
        System.out.println("The median is: " + math.median);
    }
}
ArrayList<Integer> a = new ArrayList<Integer>();
Scanner b = new Scanner(System.in);

Integer c = null;
while (a.size() != 10) {
    System.out.println("Please input a number in the range 1-19 :> ");
    c = b.nextInt();
    while (c < 1 || c > 19)
    {
        System.out.println("Not in the range 1-19, please try again :> ");
        c = b.nextInt();
    }
    a.add(c);
}

System.out.println("\nArray contents");
for (int i=0; i<10; i++) {
    System.out.print((a.get(i) < 10 ? " " : "") + a.get(i) + "  ");
}
package p2;

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

public class TicTacToe {

    public static void main(String[] args) {
        Scanner kybd = new Scanner(System.in);
        int[] numbers = new int[10];
        int sum = 0;
        System.out.println("Enter 10 integers: ");
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = kybd.nextInt();
            sum += numbers[i];
        }

        Arrays.sort(numbers);
        System.out.println("The minimum number is: " + numbers[0]);
        System.out.println("The maximum number is: " + numbers[9]);
        System.out.println("The average value is: " + sum / numbers.length);
        System.out.println("The median is: " + median(numbers));
    }

    public static double median(int[] m) {
        int middle = m.length / 2;
        if (m.length % 2 == 1) {
            return m[middle];
        } else {
            return (m[middle - 1] + m[middle]) / 2.0;
        }
    }
}