簡體   English   中英

將多個類同時用於全部輸入時出錯

[英]Error when Using multiple classes with taking inputs all at once

如果我只使用一個類,它會完美工作,但是當我使用多個類時,我面臨一個問題,其中,當我將輸入全部作為批處理(復制粘貼)時,它不起作用(仍在等待一些更多的輸入,卻什么也沒做),但是當我手動輸入每個輸入時,效果就很好。

因此,當我引入一個新類時,這個問題就開始了,所以我猜想在與Scanner類一起使用時,該類或繼承有問題。

請比較,讓我知道錯誤

注意:這是針對我的大學實驗室的,所以我不能在這里使用文件。

btw,MyInputs是

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 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
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM