2016-05-17 58 views
-4
package com.Bruno; 

import java.util.Scanner; 
public class Main { 


    public static void main(String[] args) { 
     Scanner reader = new Scanner(System.in); 

     System.out.print("How Many:"); 
     int numberOne = Integer.parseInt(reader.nextLine()); 




     while ((numberOne >=0) && (numberOne <=7)) { 
      System.out.print(printText() + "\n"); 
      numberOne--; 
     } 

    public static void printText() { 
     System.out.println("In the beginning there were the swamp, the hoe and Java." + "/n"); 
    } 



    } 
} 
+3

你好,歡迎堆棧溢出!請編輯您的問題,以包含您要問什麼問題的一些說明。 –

+1

在System.out.print中調用System.out.println。學到了一些東西。 –

+0

只需調用'printText();' – shmosel

回答

1
  1. 的方法你嘗試調用printText()方法爲嵌套這是不允許的。
  2. 即使您將printText()從main中取出,它也會因爲類型不匹配而發生錯誤。 printText()打印到輸出流並不返回任何內容。它不能直接在另一種方法中使用,因爲它什麼都不返回。
  3. 使用返回類型可以輸入到另一個方法的方法應該解決的問題

代碼:

import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     Scanner reader = new Scanner(System.in); 

     System.out.print("How Many:"); 
     int numberOne = Integer.parseInt(reader.nextLine()); 

     while ((numberOne >= 0) && (numberOne <= 7)) { 
      System.out.print("" + printText() + "\n"); 
      numberOne--; 
     } 

    } 

    public static String printText() { 
     return "In the beginning there were the swamp, the hoe and Java." + "/n"; 
    } 
}