2017-02-04 3 views
-4

drucken Ich möchte "Punkt" in "printMethod" -Methode drucken. Wie "Spielername ist Michael Punkt war 2000 und Spieler Position 1" Wie kann ich das tun? Gibt es einen geeigneten Weg, dies leicht zu tun? Ich kann es tun, indem ich eine neue Methode erstelle, aber es ist so gekritzelt.Ich möchte Eingangswert in Methode

 public class Test{ 
     public static void main(String[] args){ 
     int returnPosition = calculatePosition(2000); 
     printmethod("Michael" , returnPosition); 

     returnPosition = calculatePosition(900); 
     printmethod("Mark" , returnPosition); 

     returnPosition = calculatePosition(800); 
     printmethod("Halk" , returnPosition); 

     returnPosition = calculatePosition(700); 
     printmethod("Albion" , returnPosition); 

     returnPosition = calculatePosition(600); 
     printmethod("Cristine" , returnPosition); 

     returnPosition = calculatePosition(500); 
     printmethod("Emma" , returnPosition); 

     returnPosition = calculatePosition(400); 
     printmethod("Prince" , returnPosition); 

     returnPosition = calculatePosition(300); 
     printmethod("Jason" , returnPosition); 

     returnPosition = calculatePosition(200); 
     printmethod("Tim" , returnPosition); 

     returnPosition = calculatePosition(100); 
     printmethod("Bob" , returnPosition); 

     returnPosition = calculatePosition(50); 
     printmethod("Jackson" , returnPosition); 
    } 
    public static void printMethod(String playerName, int position){ 
     System.out.println("Player name is "+playerName+ " Point was " +point+ " and player position " +position); 
    } 
    public static int calculatePosition(int position){ 
     if (position > 1000){ 
      return 1; 
     }else if (position > 500 && position < 1000){ 
      return 2; 
     }else if (position > 100 && position < 400){ 
      return 3; 
     }else{ 
      return 4; 
     } 
    } 
} 
+0

Sie können in Punkt passieren 'print()' nach seinem Prototyp Wechsel –

+0

ich nicht understand..I Berechnungen für die Position zu sehen, aber wo und wie Sie ‚Punkt‘ berechne? – smoggers

+0

Punkt wurde noch nicht verwendet.Punkt bedeutet meine Eingabedaten (2000,900,800 usw.) .Ich möchte Punkt in "PrintMethod" -Methode für die Ausgabe verwenden. @smoggers –

Antwort

0

Ich habe meine Frage Lösung nach einer Menge Gedanken und überprüfen Sie Ihren Code.Vielen Dank, alle.

import java.util.Scanner; 
    public class Practise { 
    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Please enter player name: "); 
    String playerName = input.nextLine(); 
    System.out.println("Please enter player score: "); 
    int score = input.nextInt(); 
    int finalScore = calculateSection(score); 
    displayM(playerName , finalScore , score); 
} 

public static void displayM(String playerName,int finalScore, int score) { 
    System.out.println("Player name is " + playerName + " score was " + score+ " position is "+finalScore); 
} 

public static byte calculateSection(int score) { 
    if (score >= 1000) { 
     return 1; 
    } else if (score >= 500 && score < 1000) { 
     return 2; 
    } else if (score >= 400 && score < 500) { 
     return 3; 
    } else { 
     return 4; 
    } 

} 
0

Sie können ein anderes Attribut in Ihrer Methode wie folgen passieren:

public static void printMethod(String playerName, int position, Integer point){ 
    System.out.println("Player name is " + playerName + " Point was " + point + " and player position " + position); 
} 

Hinweis

nicht Ihre Klassennamen public class main { die Namen ändern oder die ersten Buchstaben in Großbuchstaben machen wie dies public class Main { kann dies ein Problem machen.

so in Ihre Hauptmethode sollten Sie Ihre Methode wie folgt nennen:

int returnPosition = calculatePosition(2000); 
    printmethod("Michael" , returnPosition, 2000); 

Dieses zum Beispiel gedruckt wird:

Spielername ist Michael Punkt war 2000 und Spielerposition xxxx

EDIT

Wenn Sie nicht über ein anderes Attribut in Ihrer Methode übergeben wollen Sie in wie ein Attribut in der Klasse wie folgt speichern:

public class Help { 

    //attribute to put value of your point 
    public static int point; 

    public static void main(String args[]) throws IOException { 
     int returnPosition = calculatePosition(2000); 
     point = 200; 
     printMethod("Michael", returnPosition); 
    } 

    public static void printMethod(String playerName, int position){ 
     System.out.println("Player name is " + playerName + " Point was " + point + " and player position " + position); 
    } 
} 
+0

ok @Mr Rango überprüfen meine Antwort jetzt habe ich die Art geändert Integer –

+0

Danke dort für Ihre answer.Is jede andere Möglichkeit, nur den Punkt nur einmal einzugeben? @YCL_L –

+0

was meinst du @MrRango? Ich verstehe dich nicht? –

0

Ich würde lieber so tun

import java.util.Map; 
import java.util.TreeMap; 

public class Test { 

    public static void main(String[] args) { 

     Map<String, Integer> playersList = new TreeMap<>(); 

     playersList.put("Michael", 2000); 
     playersList.put("Mark", 900); 
     playersList.put("Halk", 800); 
     playersList.put("Albion", 700); 
     playersList.put("Cristine", 600); 
     playersList.put("Emma", 500); 
     playersList.put("Prince", 400); 
     playersList.put("Jason", 300); 
     playersList.put("Tim", 200); 
     playersList.put("Bob", 100); 
     playersList.put("Jackson", 50); 

     playersList.forEach((playerName, points) -> { 

      printMethod(playerName, points, calculatePosition(points)); 
     }); 
    } 

    public static void printMethod(String playerName, int point, int position) { 

     System.out.println("Player name is " + playerName + " Point was " + point + " and player position " + position); 
    } 

    public static int calculatePosition(int points) { 

     int position = 4; 

     if (points > 1000) { 
      position = 1; 
     } else if (points > 500 && points < 1000) { 
      position = 2; 
     } else if (points > 100 && points < 400) { 
      position = 3; 
     } 

     return position; 
    } 
} 
+0

Vielen Dank für Ihre brillante Antwort. Vielen Dank. @Ravi MCA –