Question: Hi, if you can please translate this code from java to C++. THANK YOU import java.io.*; import java.util.Random; class NeverAbove50 { public static void main(String
Hi, if you can please translate this code from java to C++. THANK YOU
import java.io.*;
import java.util.Random;
class NeverAbove50
{
public static void main(String arg[])throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
String playerName="Player";
do
{
System.out.println();
System.out.println(" --------------------------------");
System.out.println("| NEVER OVER 50 GAME |");
System.out.println(" --------------------------------");
System.out.println("1. Game Rules");
System.out.println("2. Edit Player Details");
System.out.println("3. Play");
System.out.println("4. Exit");
System.out.print(" Please pick an option: ");
int ch=Integer.parseInt(br.readLine());
if(ch==4)
break;
switch(ch)
{
case 1:
System.out.println();
System.out.println(" --------------------------------");
System.out.println("| GAME RULES |");
System.out.println(" --------------------------------");
System.out.println("The idea of the never over 50 game is to allow the users to add up a random number in the range of to 20 each turn to get a final number in the range of 40 to 50. If the player's score is less than 50 after each turn, they can choose to continue. If they choose to continue, the number of turns will increase by 1. The player's turn will automatically stop if the final score exceeds 50 or has lost the game with 0 points. The player also loses the game and gets 0 points if their final score is less than 40 after deciding to end their turns. If the player's final score is in the range of 40 to 50, then the final point is equal to the final score subtracting the number of turns the user took to get the final score.");
System.out.print(" Press 0 to go back to the main menu: ");
int ch1=Integer.parseInt(br.readLine());
while(ch1!=0)
{
System.out.print(" Wrong option, press 0 to go back to the main menu: ");
ch1=Integer.parseInt(br.readLine());
}
break;
case 2:
System.out.println();
System.out.println(" --------------------------------");
System.out.println("| PLAYER DETAILS |");
System.out.println(" --------------------------------");
while(true)
{
System.out.println("Player name: "+playerName);
System.out.print(" Press 1 to edit or press 0 to go back to the main menu: ");
int ch2=Integer.parseInt(br.readLine());
if(ch2==1)
{
System.out.print(" Please enter new details: ");
playerName=br.readLine();
System.out.println("Player details changed");
continue;
}
while(ch2!=0)
{
System.out.print(" Wrong option, press 1 to edit or press 0 to go back to the main menu: ");
ch2=Integer.parseInt(br.readLine());
if(ch2==1)
{
System.out.print(" Please enter new details: ");
playerName=br.readLine();
System.out.println("Player details changed");
break;
}
}
if(ch2==0)
break;
}
break;
case 3:
while(true)
{
System.out.println();
System.out.println(" ----------------------");
System.out.println("| PLAY |");
System.out.println(" ----------------------");
int turn=1;
int score=0;
while(true)
{
int min = 1;
int max = 20;
Random random = new Random();
int value = random.nextInt(max + min) + min;
score+=value;
System.out.println("Turn "+turn+", random number is "+value+" and your score is "+score);
turn++;
System.out.print("Press \'y\' to continue or \'s\' to stop: ");
char c=br.readLine().charAt(0);
boolean res=false;
score-=turn-1;
if(score>=40&&score<=50)
res=true;
if((c=='s')||(c=='y'&&score>50))
{
System.out.println(" "+playerName+", You have "+(res?"won":"lost")+" the game, "+(res?score:0)+" is your final point");
break;
}
}
System.out.print(" Press 1 to play the game again or press 0 to go back to the main menu: ");
int ch3=Integer.parseInt(br.readLine());
if(ch3==1)
{
continue;
}
while(ch3!=0)
{
System.out.print(" Wrong option, press 1 to play the game again or press 0 to go back to the main menu: ");
ch3=Integer.parseInt(br.readLine());
if(ch3==1)
{
break;
}
}
if(ch3==0)
break;
}
break;
default:
System.out.println(" Your chosen option is not listed, please try again: ");
}
}while(true);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
