战争-纸牌游戏-带赌注-Java
我的问题是在我的交易中尝试和抓住;方法。不确定为什么会在“下一步”下出现错误。在控制台框中,您将看到它是如何运行的,它在向玩家发了几张牌后停止,这时它应该发完所有的牌并要求再次玩。我在监督什么战争-纸牌游戏-带赌注-Java,java,try-catch,Java,Try Catch,我的问题是在我的交易中尝试和抓住;方法。不确定为什么会在“下一步”下出现错误。在控制台框中,您将看到它是如何运行的,它在向玩家发了几张牌后停止,这时它应该发完所有的牌并要求再次玩。我在监督什么 import java.util.Scanner; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.Sim
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.sql.Timestamp;
import java.util.Date;
public class finalproject {
private static final String reader = null;
private static int X = 0;
private static int Y = 0;
private static String Names = "";
private static File outfile = new File("/Users/josephbosco/" + Names +".txt");
private static int AmountOfPlayers = 0;
private static int AmountOfDecks = 0;
public static void main(String[] args) {
boolean numberError = true;
int Y = 0;
do {
Deal(); //This is line 30
Scanner input = new Scanner(System.in);
System.out.println( " Would you like to play again? Press 1 or press any other key ");
Y = input.nextInt();
try {
System.out.print(" Would you like to play again? Press 1 or press any other key ");
String TheNames = Scanner.next; //Read into a string
Y = Integer.parseInt(TheNames.trim());
numberError = false; //if we haven't bailed out, then the number must be valid.
} catch(Exception e) {
System.out.println("The value you provied is invalid...Please try again"); //error message is for user
numberError = true; //Uh-oh...We have a problem.
}
} while (Y == 1) ;
}
public static void Print() {
Scanner myScanner1 = new Scanner(System.in);
String fileName = "/Users/JosephBosco/" + ".txt";
String TimeStamp = CurrentTimeStamp("");
printToFile ( fileName, TimeStamp +"\r\n");
}
public static void printToFile ( String myFileName, String message) {
try {
File outfile = new File("/Users/josephbosco/" + Names +".txt");
if (!outfile.exists()); {
outfile.createNewFile();
FileWriter fw = new FileWriter(outfile.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
System.out.println( " New Player, begins with $100 ");
bw.write ( Names + " $100 ");
bw.flush();
bw.close();
}
}
catch (IOException e) {
System.out.println(" Uh oh! Something happened!!");
e.printStackTrace(); }
}
public static void Deal() {
Scanner myScanner = new Scanner(System.in);
boolean numberError = false;
do {
try {
System.out.println(" Welcome players to War with betting! How many players are there?");
String enteredString = myScanner.next();
AmountOfPlayers = Integer.parseInt(enteredString.trim());
numberError = false;
}
catch (Exception e) {
System.out.println( " That number is invalid! Please try again " + e);
numberError = true;
String garbage = myScanner.next();
}
} while (numberError == true);
System.out.println( " \n Thankyou, that was a valid Number ");
System.out.println( AmountOfPlayers + " Players will play War!");
String[] ListOfNames = new String[AmountOfPlayers];
for( int Y = 0; Y < ListOfNames.length; Y++) {
System.out.print(" Enter the player's name: " + ( Y+1 ) + ":");
Names = ListOfNames [Y] = myScanner.next();
Print();
}
do {
try {
System.out.println(" Okay, so, how many decks would you like to play with? " );
String enteredString = myScanner.next();
AmountOfDecks = Integer.parseInt(enteredString.trim());
numberError = false;
}
catch (Exception e) {
System.out.println( " That is an invalid Number! Try again " + e);
numberError = true;
String garbage = myScanner.next();
}
} while (numberError == true);
System.out.println( " \n That was a valid Number ");
System.out.println( AmountOfDecks + " is the amount of decks to be used!");
System.out.println( " Press enter to start game ");
new Scanner(System.in).nextLine();
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};
int[] Deck = new int[52 * AmountOfDecks]; {
for( int i = 0; i < Deck.length; i++) Deck[i] = i;
for( int i = 0; i < Deck.length; i++) {
int index = (int)(Math.random() * Deck.length);
int temp = Deck[i];
Deck[i] = Deck[index];
Deck[index] = temp;
}
for( int Q = 0; Q < AmountOfDecks; Q++) { //Line 157 has nothing on it??? its the line above the first "for"
for( int Y = 0; Y < Names.length(); Y++); {
for( int V = 0; V < Deck.length; V++ ) {
String suit = suits[Deck[V] % 4];
String number = numbers[Deck[V] % 13];
System.out.print( ListOfNames[Y] + " drew " + " : " + number + suit + "\r\n");
Y++;
}
}
}
}
}
public static final String CurrentTimeStamp ( final String TimeStamp) {
String CurrentTimeStamp = new SimpleDateFormat(new Timestamp( new java.util.Date().getTime()).toString()).format(new java.util.Date());
return CurrentTimeStamp;
}
}
第一句话对我来说没有意义,也不应该是这样的
Names = Names [Y] = myScanner.next();
您正在将
String[]Names
设置为单个字符串值 “好吧,这是我的代码,我不确定它到底出了什么问题。”这或多或少是一种保证,没有人会看你的东西。您应该在错误所在的位置发布方法。。。因为在当前状态下,我们看不到哪一行是101和28这一行毫无意义:private static final String reader=null代码>不要破坏你的帖子;您已将其内容提交给此网站,但它仍将保留。因此,您认为您有权提供帮助,并忽略您要求的网站规则?说到势利……我真的弄明白了,我不得不把名字改成名字列表,然后我就开始工作了。谢谢你,酷。您的代码仍然很混乱,但我会让您处理的。;)是的,我是新来的,这是我介绍课的最后一个作业,非常紧张,hahaI修复了一堆东西,我编辑并发布了新代码,我唯一的错误是顶部的“交易”方法中的try-and-catch。我在监督什么吗?当我点击错误时,我没有得到任何建议。永远不要更改原始问题的代码,因为它会使答案无效。总是为新问题提出新问题。
Names = Names [Y] = myScanner.next();