将多个类同时用于全部输入时出错

时间:2019-01-13 03:58:37

标签: java java.util.scanner

如果我仅使用一个类,则效果很好,但是当我使用多个类时,我面临一个问题,其中,当我将输入全部作为批处理(复制粘贴)时,它不起作用(仍然等待更多输入,但什么也不做),但是当我手动输入每个输入时,效果很好。

所以,这个问题是在我引入一个新类时开始的,所以我猜想在与Scanner类一起使用时,该类或继承有问题。

请比较一下,让我知道错误

注意:这是给我的大学实验室用的,所以我不能在这里使用文件。

btw,MyInputs是

5

5 0

2 9 -10 25 1

5 1

2 9 -10 25 1

5 1

2 9 -10 25 1

5 0

2 9 -10 25 1

5 1

2 9 -10 25 1

预期产量

5.400000

4.000000

4.000000

5.400000

4.000000

codeWithSingleClass-完美的作品

import java.io.*;
import java.lang.*;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    int nooftestCases=scanner.nextInt();

    while(nooftestCases>0) {
        int n,k;
        int[] array = new int[20];
        int sumWithOutRemoval=0 , sumWithRemoval=0;
        n = scanner.nextInt();
        k = scanner.nextInt();
        sumWithOutRemoval = 0;
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
            sumWithOutRemoval += array[i];
        }
        if (k == 0) {
            double finalAns = (double) sumWithOutRemoval / n;
            System.out.println(String.format("%.6f", finalAns));
        } else {
            for (int i = 0; i < n; i++) {
                for (int j = i; j < n; j++) {
                    if (array[i] < array[j]) {
                        int temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
            sumWithRemoval = 0;
            for (int i = 1; i < n - 1; i++) {
                sumWithRemoval += array[i];
            }
            double finalAns = (double) (sumWithRemoval / (n - (2 * k)));
            System.out.println(String.format("%.6f", finalAns));
        }
        nooftestCases--;
    }
  }
 }



--->codeWithMultipleClasses-hasIssues<----

import java.io.*;
import java.lang.*;
import java.util.Scanner;

class Sample {

 static int n,k;
 static int[] array = new int[20];
 static int sumWithOutRemoval , sumWithRemoval;

 public void getDetails(){
    Scanner scanner2=new Scanner(System.in);
    n = scanner2.nextInt();
    k = scanner2.nextInt();
    sumWithOutRemoval = 0;
    for (int i = 0; i < n; i++) {
        array[i] = scanner2.nextInt();
        sumWithOutRemoval += array[i];
    }
  }
 public void displayDetails(){
    if (k == 0) {
        double finalAns = (double) sumWithOutRemoval / n;
        System.out.println(String.format("%.6f", finalAns));
    }
    else {
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                if (array[i] < array[j]) {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
        sumWithRemoval = 0;
        for (int i = 1; i < n - 1; i++) {
            sumWithRemoval += array[i];
        }
        double finalAns = (double) (sumWithRemoval / (n - (2 * k)));
        System.out.println(String.format("%.6f", finalAns));
    }
 }
 }
 public class Main extends Sample {
 public static void main(String[] args) {
     Scanner scanner=new Scanner(System.in);
    int nooftestCases=scanner.nextInt();
    Sample objname= new Sample();
    while(nooftestCases>0) {
        objname.getDetails();
        objname.displayDetails();
        nooftestCases--;
     }
   }
 }

1 个答案:

答案 0 :(得分:0)

如果我正确理解,您将收到如下输入:

1 2 3 4 5 6

完成后按Enter键。 如果是这种情况,那么您需要将输入解析为字符串并将其拆分为参数:

   public static void main(String[] args){
       Scanner scanner=new Scanner(System.in);
       String input = scanner.nextLine();
       String[] arguments = input.split("[ \n]");
       System.out.println("First argument:"+arguments[0]);
       System.out.println("Last argument:"+arguments[arguments.length - 1]);
       //do something with the arguments
   }