New Posts  All Forums:Forum Nav:

Java Homework Help

post #1 of 4
Thread Starter 
So I'm making a Yahtzee game for my java class with lots of methods and such, so close to being finished but this one part is causing me quite the headache and I would love help. I won't bother posting all my code since it shouldn't be necessary...and I don't want other students copying it = D.

This is my method that allows the user to choose which dice to throw again (if any) and I want it to end reading the input when the user presses the enter key. I understand that the Scanner class can't record keystrokes and I need to somehow incorporate nextLine() but I can't for the life of me figure it out.....any help or input would be great, thanks.
Code:
public static void throwAgain(int dice){
                Scanner input = new Scanner(System.in);
                int a = 0, b = 0;
                int redo = new int;
                System.out.print("List which die to throw again: ");
                 while (input.hasNextInt() == true){
                       b = input.nextInt();
                               while(b  5 || redo.length > 6){
                                        System.out.println("Illegal die!");
                                        System.out.print("List which die to throw again: ");
                                        b = input.nextInt();
                              }
                              redo = b;
                              a++;
                              }
                      for(int i = 0; i < 5; i++){
                            if(redo == 1)
                                   dice = (int)(6*Math.random() + 1);
                           else if(redo == 2)
                                   dice = (int)(6*Math.random() + 1);
                           else if(redo == 3)
                                   dice = (int)(6*Math.random() + 1);
                          else if(redo == 4)
                                  dice = (int)(6*Math.random() + 1);
                          else if(redo == 5)
                                   dice = (int)(6*Math.random() + 1);
                }
}

Ouput should look like:
Welcome to Yahtzee!
1 1 1 6 6
List which die to throw again: 1 2 3
5 2 5 6 6
This is chance.
Would you like to throw again(yes/no)? no
Edited by Mullinz - 11/27/11 at 11:53am
post #2 of 4
I believe you would benefit from an InputStreamReader and using the isDigit() method on the resulting object; If the input is a digit then just re-roll said dice, if it's not a digit finish the program.
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #3 of 4
Thread Starter 
thanks I'll take a look at that and see if it would help me out
post #4 of 4
Quote:
Originally Posted by Chris++ View Post

I believe you would benefit from an InputStreamReader and using the isDigit() method on the resulting object; If the input is a digit then just re-roll said dice, if it's not a digit finish the program.

I second this idea. Then just store each digit, to determine which dice to roll again
Zeus
(17 items)
 
Apollo
(6 items)
 
 
CPUMotherboardGraphicsGraphics
Intel i7 2600k Asus P8Z68 PRO  EVGA 560 ti DS Superclocked  EVGA 560 ti DS Superclocked 
RAMHard DriveHard DriveOptical Drive
16GB Corsair Vengeance Crucial M4 64GB SSD (System) Samsung Spinpoint F3 1TB (Storage) LG Blu-Ray 
CoolingOSMonitorKeyboard
Corsair H100 Windows 7 Professional (64-bit) ASUS VS238H-P DASkeyboard Ultimate 
PowerCaseMouseMouse Pad
Corsair HX850 Corsair Carbide 400r Logitech G9x Iowa State Cyclones 
Audio
Logitech z506 
CPUMotherboardGraphicsRAM
Intel Pentium 4 (520 Prescott) Shuttle Inc FD30V10 nVidia GeForece 210 (GT128) 2 GB Kingston 
Hard DriveOS
Seagate Barracuda Green  Windows 7 32-bit 
  hide details  
Reply
Zeus
(17 items)
 
Apollo
(6 items)
 
 
CPUMotherboardGraphicsGraphics
Intel i7 2600k Asus P8Z68 PRO  EVGA 560 ti DS Superclocked  EVGA 560 ti DS Superclocked 
RAMHard DriveHard DriveOptical Drive
16GB Corsair Vengeance Crucial M4 64GB SSD (System) Samsung Spinpoint F3 1TB (Storage) LG Blu-Ray 
CoolingOSMonitorKeyboard
Corsair H100 Windows 7 Professional (64-bit) ASUS VS238H-P DASkeyboard Ultimate 
PowerCaseMouseMouse Pad
Corsair HX850 Corsair Carbide 400r Logitech G9x Iowa State Cyclones 
Audio
Logitech z506 
CPUMotherboardGraphicsRAM
Intel Pentium 4 (520 Prescott) Shuttle Inc FD30V10 nVidia GeForece 210 (GT128) 2 GB Kingston 
Hard DriveOS
Seagate Barracuda Green  Windows 7 32-bit 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming