0
我使用冒泡排序方法按順序對輸入數字進行排序(從最小到最大),並在自己的方法中使用。然後,在下一個方法中(我需要按順序編號),似乎忽略了我在前面的方法中對數字進行了排序。我是否沒有正確地調用方法或什麼? 謝謝!爲什麼我的變量的方法不一樣?
import java.util.Scanner;
public class hw6 {
public static int getOrder(int n1, int n2, int n3, int n4, int n5) {
Scanner in = new Scanner(System.in);
if (n1>n2){
int t = n1;
n1 = n2;
n2 =t;
}
if (n2>n3){
int t = n2;
n2 = n3;
n3 =t;
}
if (n3>n4){
int t = n3;
n3 = n4;
n4 =t;
}
if (n4>n5){
int t = n4;
n4 = n5;
n5 =t;
}
if (n1>n2){
int t = n1;
n1 = n2;
n2 =t;
}
if (n2>n3){
int t = n2;
n2 = n3;
n3 =t;
}
if (n3>n4){
int t = n3;
n3 = n4;
n4 =t;
}
if (n1>n2){
int t = n1;
n1 = n2;
n2 =t;
}
if (n2>n3){
int t = n2;
n2 = n3;
n3 =t;
}
if (n1>n2){
int t = n1;
n1 = n2;
n2 =t;
}
System.out.println(n1+" "+n2+" "+n3+" "+n4+" "+n5);
return n1+n2+n3+n4+n5;//cards now in order from least to greatest
}
public static int getRank(int n1, int n2, int n3, int n4, int n5) {
int order;
int rank = 1;
order = getOrder(n1, n2, n3, n4, n5);
if (n1 == n2 && n1 == n3 && n1 == n4 ||
n2 == n3 && n2 == n4 && n2 == n5)
System.out.println("Four of a kind");
else if (n1 == n2 && n1 == n3 && n4 == n5 ||
n1 == n2 && n3 == n4 && n3 == n5)
System.out.println("Full house");
else if (n2 == n1+1 && n3 == n2+1 && n4 == n3+1 && n5 == n4+1)
System.out.println("Straight");
else if (n1 == n2 && n1 == n3 || n2 == n3 && n2 == n4 ||
n3 == n4 && n3 == n5)
System.out.println("Three of a kind");
else
System.out.println("A high card");
return rank;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1, n2, n3, n4, n5; //input
int order;
int rank;
System.out.print("Enter (1-13):");
n1 = in.nextInt();
System.out.print("Enter (1-13):");
n2 = in.nextInt();
System.out.print("Enter (1-13):");
n3 = in.nextInt();
System.out.print("Enter (1-13):");
n4 = in.nextInt();
System.out.print("Enter (1-13):");
n5 = in.nextInt();
order = getOrder(n1, n2, n3, n4, n5);
rank = getRank(n1, n2, n3, n4, n5);
}
}
說實話,不完全確定。這就是我被教導要做的事情。從來沒有嘗試過任何其他方式。 – ToonLink
順便說一句,你應該考慮遵循Java命名約定「類名應以大寫字母開頭」 – Prateek