Created
January 29, 2012 20:54
-
-
Save brikis98/1700592 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: Ruby, Day 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
to_guess = rand(10) + 1 | |
puts "Try to guess a number between 1 and 10." | |
begin | |
print "Enter your guess: " | |
guess = gets.to_i | |
puts "Too low!" if guess < to_guess | |
puts "Too high!" if guess > to_guess | |
end while guess != to_guess | |
puts "Correct!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Scanner; | |
public class GuessNumber { | |
public static void main(String[] args) { | |
Random generator = new Random(); | |
int toGuess = generator.nextInt(10) + 1; | |
int guess; | |
Scanner scanner = new Scanner(System.in); | |
try { | |
System.out.println("Try to guess a number between 1 and 10."); | |
do { | |
System.out.print("Enter your guess: "); | |
guess = scanner.nextInt(); | |
if (guess < toGuess) { | |
System.out.println("Too low!"); | |
} else if (guess > toGuess) { | |
System.out.println("Too high!"); | |
} | |
} while (guess != toGuess); | |
System.out.println("Correct!"); | |
} finally { | |
scanner.close(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Hello, Ruby".index "Ruby" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Hello, world" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10.times { puts "Jim" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.upto(10) { |i| puts "This is sentence number #{i}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See Seven Languages in Seven Weeks: Ruby, Day 1 for more information.