Glasgow University Bioinformatics forum

Glasgow University Bioinformatics forum, for the MRes bioinformatics students of the University of Glasgow
 
HomeHome  ­FAQFAQ  ­SearchSearch  ­MemberlistMemberlist  ­UsergroupsUsergroups  ­RegisterRegister  ­Log inLog in  
Post new topic   Reply to topicShare | 
 

 Code for Lab 6 (discussing)

View previous topic View next topic Go down 
AuthorMessage
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Code for Lab 6 (discussing)   Fri Oct 03, 2008 3:33 pm

the code for Lavb 6 seems to be much less than Lab 5


import java.util.*;
//import java.lang.*; //not necessary to use java.lang.* here?
import FormatIO.*;


public class Lab6 {
public static void main(String[] arg){
//get a random number
//compare the input number with the random number
//make comment if the input number is too high or too law
//stop guessing if the number is right
//print out the number of steps;

int step;
int game = 0;
double aveScore = 0.0;
Random rand = new Random();
Console con = new Console();
boolean playAgain = true;

while(playAgain){
step = 0;
int num = Math.abs(rand.nextInt())%10;
System.err.print(num);
con.println("Please enter a number from 0 - 9 ");

game = game + 1;

for(;Wink{
int a=con.readInt();
step = step+1;//recording the step while looping
if(a>num)
con.println("Sorry! You guess wrongly. " +
"The number you entered is too high");
else if(a<num)
con.println("Sorry! You guess wrongly. " +
"The number you entered is too low");
else{
break;
}
}

con.println("congrutulations! You entered the right number");
con.println(String.format("you took %d steps to get the right number.",step));

aveScore = (aveScore * (game-1) + step)/game;

con.println("Do you want to play another game?(yes/no)");
String s = con.readWord();
if(s.equalsIgnoreCase("no"))
playAgain=false;
}

con.println(String.format("Your average score of the game is %-4.1f steps",aveScore));
}
}
Back to top Go down
View user profile
Mike



Posts: 25
Join date: 2008-09-18

PostSubject: Re: Code for Lab 6 (discussing)   Sun Oct 05, 2008 9:47 pm

I don't know is there any use to post a code for this exercise if it is longer and worse than already posted, but anyway...

Code:
// Program-game "Try to guess the right number"

import FormatIO.*;
import java.util.*;
import java.lang.*;

public class Exx3 {
   public static void main(String[] args)
   {
      Console con = new Console();
      
      int userNumber = 0;
      int i = 0;
      int j = 0;
      int totalI = 0;
      int averageAttempts = 0;
      
      boolean validNewGame = false;
      
      while (!validNewGame)
      {
         i = 0;
         // generating a random number
         Random rand = new Random();
         int num = Math.abs(rand.nextInt()) % 10;
         System.err.println(num); // Trace the number
      
         String answer = "";
            
         boolean validRightAnswer = false;
      
         while (!validRightAnswer)
         {
                  // proposal to user to play the game and guess the number
         if (i > 0 && userNumber > num && userNumber >= 0 && userNumber <10)
            {
            con.println("You gave too high number, try again!");
            }
         else if (i > 0 && userNumber < num)
            {
            con.println("You gave too low number, try again!");
            }
         else
            {
         con.println("Try to guess the number (from 0 to 9). Enter it: ");
            }
      userNumber = con.readInt();
      
      
      // test for the right range of numbers
      if (userNumber < 0 || userNumber > 9)
         {
         con.println("You should enter a number from 0 to 9!");
         i--;
         }      
      i++;
      // test if the user number equals the random number, proposed by the computer
      validRightAnswer = (userNumber == num);
         }
      con.println("Congratulations! You hit it for " + i +" attempt(s)");
      totalI += i;
      con.println("Do you want to play again? Yes/No: ");
      answer = con.readWord();
      System.out.println(answer);
      
      validNewGame  = (!answer.equalsIgnoreCase("Yes"));
      j++;
      validRightAnswer = false;
      }
      averageAttempts = (int)java.lang.Math.round((totalI / (double)j));
      con.println(String.format("The average number of attempts you need to guess a right number is %d", averageAttempts));
      }
   }
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: Code for Lab 6 (discussing)   Sun Oct 05, 2008 10:57 pm

Mike, I dont think there is a best way to finish our tasks. All the solution which can solve the problem are good ones.
To see what other people think about the same problem can help us to broad our minds, I suppose ..

Obviously, you did better to control the range of input numbers(highlighted in red). It's proper to do so.




Mike wrote:
I don't know is there any use to post a code for this exercise if it is longer and worse than already posted, but anyway...

// Program-game "Try to guess the right number"

import FormatIO.*;
import java.util.*;
import java.lang.*;

public class Exx3 {
public static void main(String[] args)
{
Console con = new Console();

int userNumber = 0;
int i = 0;
int j = 0;
int totalI = 0;
int averageAttempts = 0;

boolean validNewGame = false;

while (!validNewGame)
{
i = 0;
// generating a random number
Random rand = new Random();
int num = Math.abs(rand.nextInt()) % 10;
System.err.println(num); // Trace the number

String answer = "";

boolean validRightAnswer = false;

while (!validRightAnswer)
{
// proposal to user to play the game and guess the number
if (i > 0 && userNumber > num && userNumber >= 0 && userNumber <10)
{
con.println("You gave too high number, try again!");
}
else if (i > 0 && userNumber < num)
{
con.println("You gave too low number, try again!");
}
else
{
con.println("Try to guess the number (from 0 to 9). Enter it: ");
}
userNumber = con.readInt();


// test for the right range of numbers
if (userNumber < 0 || userNumber > 9)
{
con.println("You should enter a number from 0 to 9!");
i--;

}
i++;
// test if the user number equals the random number, proposed by the computer
validRightAnswer = (userNumber == num);
}
con.println("Congratulations! You hit it for " + i +" attempt(s)");
totalI += i;
con.println("Do you want to play again? Yes/No: ");
answer = con.readWord();
System.out.println(answer);

validNewGame = (!answer.equalsIgnoreCase("Yes"));
j++;
validRightAnswer = false;
}
averageAttempts = (int)java.lang.Math.round((totalI / (double)j));
con.println(String.format("The average number of attempts you need to guess a right number is %d", averageAttempts));
}
}
Back to top Go down
View user profile
 

Code for Lab 6 (discussing)

View previous topic View next topic Back to top 
Page 1 of 1

Permissions of this forum:You cannot reply to topics in this forum
Glasgow University Bioinformatics forum :: IT Strand :: Programming-
Post new topic   Reply to topic