We appreciate your visit to Task 1 Random Integer Guessing Game Write a program that generates a random integer from 1 to 10 inclusive and asks the user to guess. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!
Answer :
1) Generating a random integer from 1 to 10 (inclusive) and asks the user to guess it and tell the user what the number was and how far they were from itimport java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random random = new Random();
int randomNumber = random.nextInt(10) + 1;
Scanner scanner = new Scanner(System.in);
System.out.println("Guess the number between 1 and 10");
int userGuess = scanner.nextInt();
System.out.println("The number was " + randomNumber);
System.out.println("You were off by " + Math.abs(userGuess - randomNumber));
}
}
2) Taking a number of seconds as an integer command-line argument, and prints the number of years, days, hours, minutes, and seconds it's equal toimport java.util.Scanner;
public class Main {
public static void main(String[] args) {
int totalSeconds = Integer.parseInt(args[0]);
int years = totalSeconds / (60 * 60 * 24 * 365);
totalSeconds -= years * (60 * 60 * 24 * 365);
int days = totalSeconds / (60 * 60 * 24);
totalSeconds -= days * (60 * 60 * 24);
int hours = totalSeconds / (60 * 60);
totalSeconds -= hours * (60 * 60);
int minutes = totalSeconds / 60;
totalSeconds -= minutes * 60;
int seconds = totalSeconds;
System.out.printf("%d years, %d days, %d hours, %d minutes, %d seconds", years, days, hours, minutes, seconds);
}
}
3) Drawing the board for a tic-tac-toe game in progress import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JComponent;
public class DrawTicTacToe extends JComponent {
private int xRow, xCol, oRow, oCol;
public DrawTicTacToe(int xRow, int xCol, int oRow, int oCol) {
this.xRow = xRow;
this.xCol = xCol;
this.oRow = oRow;
this.oCol = oCol;
}
public void paint(Graphics g) {
// Draw border
g.setColor(Color.BLACK);
g.drawRect(50, 50, 300, 300);
// Draw lines
g.drawLine(150, 50, 150, 350);
g.drawLine(250, 50, 250, 350);
g.drawLine(50, 150, 350, 150);
g.drawLine(50, 250, 350, 250);
// Draw X
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.PLAIN, 100));
int x1 = 50 + 15 + xCol * 100;
int y1 = 50 + 15 + xRow * 100;
int x2 = x1 + 70;
int y2 = y1 + 70;
g.drawLine(x1, y1, x2, y2);
g.drawLine(x1, y2, x2, y1);
// Draw O
g.setColor(Color.BLUE);
g.drawOval(50 + 15 + oCol * 100, 50 + 15 + oRow * 100, 70, 70);}
To know more about number visit:
https://brainly.com/question/3589540
#SPJ11
Thanks for taking the time to read Task 1 Random Integer Guessing Game Write a program that generates a random integer from 1 to 10 inclusive and asks the user to guess. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!
- Why do Businesses Exist Why does Starbucks Exist What Service does Starbucks Provide Really what is their product.
- The pattern of numbers below is an arithmetic sequence tex 14 24 34 44 54 ldots tex Which statement describes the recursive function used to..
- Morgan felt the need to streamline Edison Electric What changes did Morgan make.
Rewritten by : Barada