Random Math Operators And Asking Ten Questions With A Loop?

May 18, 2014

So my little math game is working so far. I can only do addition though. I know how to fix it if I can find out how to generate random math operators. After that I want to use a loop (or a better technique) to ask ten different math questions before the game is over.

package pkgnew;
 import java.util.Scanner;
import java.util.Random; 
public class New {
  public static void main(String args[]) {
  //Declare and construct variables
Random randomnum = new Random();

[code].....

View Replies


ADVERTISEMENT

For Loop With Random Numbers?

Jul 5, 2014

Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3,inclusive.The numbers represent the type of vehicle as stated below:

NumberVehicle Category
1Car
2Motor bikes
3Trucks

Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program. How do i do it so they will add up the sum of each vehicle?

The answer should be something like

Number of cars = X
Number of motor bikes = Y
Number of Trucks = Z

but i'm getting

Total number of vehicle:

cars
motorbikes
motorbikes
cars
Trucks
Trucks
motorbikes
motorbikes
Trucks
cars
public static void main(String[] args) {

[code]....

View Replies View Related

Do While Loop To Generate 6 Random Numbers?

Mar 2, 2014

I am currently taking a class in the field. My assignment is to generate 6 unique random numbers using a "Do While" expression. I might be mistaken but doesnt the inner loop execute first and then it works its way out? With this logic I believe my code should work but then again its not.

public class DoLottery {
public static void main (String args[]) {
int max= 10;
int random;
int random2;
int random3;

[code]...

I originally had output at the end but decided to comment it out to see if code would execute if I placed it every time the test was true.

View Replies View Related

User Must Get Three Random Numbers In A Row - Do While Loop

Feb 12, 2015

I need to fix a program in which the user must get a three random numbers in a row. They have five chances. This is what i have so far:

public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=(int)(Math.random()*(9-0));
int num2=(int)(Math.random()*(9-0));
int num3=(int)(Math.random()*(9-0));

[Code] ....

View Replies View Related

Using For Loop To Populate Array With Random Double Values

Oct 26, 2014

Write a program that creates an array that can hold 9 double values that represent baseball batting averages for a starting baseball lineup. Use a for loop to populate array with random double values in the range of 0.00 to 0.500. Recall that "double" values are what Java calls "real" numbers. Use a second for loop to print the values in the array with one number per line. Finally, use a third for loop to traverse the array and find and print the maximum batting average in the array. Note: you will need to use String.format to control the precision of a double number when you print it- Here is my code so far:

public class P2F {
public static void main (String[] args) {
double [] player= new double [9];
//player[0]= Math.random();
for (int index=0; index < player.length; index++) {

[Code] ....

When I open the terminal window I get different variations of this [D@4545c5]. I would like to know all the things I am doing wrong.

View Replies View Related

User Input True False Questions

Jan 11, 2015

Create an application that generates a quiz. Prompt for the user's first and last name, college major, and confidence in test taking (high, medium, or low). The quiz should contain at least five true/false questions about horticulture. When the user selects the correct answer, a message of positive reinforcement should be displayed. If the user selects the incorrect answer, the correct answer should be displayed with a message of constructive criticism. At the end of the quiz, display the number of correct and incorrect answers as well as the percentage of correct responses for each user.

import java.util.Scanner;
class HorticultureQuiz { 
public static void main(String[] args){

[Code].....

View Replies View Related

Creating A Quiz In Java With 3 Multiple Choice Questions

Oct 20, 2014

I have to write a short quiz that consists of 3 multiple choice questions. When the question is answered wrong I have to get it to loop back and ask the same question until the question is answered right.

I think I have the majority of my variables assigned and I have the scanner set up.

System.out.println("Question 1- Which TV sitcom recently had it's 20th anniversary?");
System.out.println("1. Two and a half men " + question1 + ".Friends 3.The Fresh Prince of Belair h.Get a hint");
answer2 = stdIn.nextInt();
if (answer2 != question1) {
System.out.println("Your answer was incorrect, please try again or pick h for a hint");
} else {
}

View Replies View Related

Create Java Application That Contains Array Of 10 Multiple-choice Questions

Dec 15, 2014

Create a java application that contains an array of 10 multiple-choice questions related to you favorite hobby. each question contains three answer choices. also create a parallel array that holds the correct answer to each question - A,B, or C. display each question and verify that the users enters only A,B, or C as the answere - if not, keep prompting the user until a valid response in entered. If the user responds to a question correctly, display "Correct!"; otherwise, display the correct answer is and the letter to the correct answer. After the user answer all the question, display the number of correct and incorrect answers.

View Replies View Related

Valid Operands And Operators

Apr 26, 2015

I am required to make a console calculator that takes the values as strings and turns them into doubles. The user needs to be able to enter something like 3+4+5*9/13 on one line and the application give the answer. As of this point I am currently on at the last section as making the user be able to enter the expression and present the answer. I have added an array list containing + - / * and the same with the numbers 1-9...I now need to actually get the information to all come together. I'm not sure how to do that. I *think* I have all the parts for it to work.

public static void main(String[] args) {
String input = ""; // initalize the string
boolean isOn = true; // used for the while loop...when false, the
// program will exit.
final String exitCommand = "Exit"; // exit command

[code]....

View Replies View Related

Truth Table For Logical Operators

Nov 11, 2014

I've created a truth table for logical operators. Now I'm supposed to alter the code so the table prints 1's and 0's instead of true or false. Here's a bit of the original code - just the header and the first line of the table.

System.out.println("p q AND OR XOR NOT");
System.out.println();
boolean p, q;
p = true; q = true;
System.out.print( p + " " + q + " " );
System.out.print( ( p & q) + " " + ( p | q) + " ");
System.out.println(( p ^ q ) + " " + ( !p) );

All of my ideas involve using an if-then statement, but that would be all kinds of inefficient and unwieldy, I think.

View Replies View Related

Java Expressions With Multiple Operators

Jan 11, 2014

Following are the expressions? I tried running them and those are the answers I got. However, I cannot figure out the working.

//c = ++a + a++ - --a - a-- + a * + a/a ; //ans is 10
//c= a* + a/a ; ans. is 1.
//c= a * +3 ; //ans is 30
//c =5+ a* +2; //25
// c= 5+ a* + a/a ; //ans. is 15.

View Replies View Related

Calculator Program - Using Decimal And Operators

Oct 20, 2014

How do I get the code to use decimals? Also whenever you input 1, 2 or 3 as one of the operators, it always does that operator as well as the 4th operator at the end. So it always does subtraction. However when you use 4 as the only operator it works perfect.

import java.util.Scanner;
public class Program05 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
 double left;
double right;
 
[Code] ......

View Replies View Related

Logic Operators And Variable Number

Jul 7, 2014

if (number % 2 == 0 && number % 3 == 0)
System.out.println(number + " is divisible by 2 and 3");

if (number % 2 == 0 || number % 3 == 0)
System.out.println(number+ " is divisible by 2 or 3");

if (number % 2 == 0 ^ number % 3 == 0)
System.out.println(number + " is divisble by 2 or 3 but not both");

I understand the logic operators and the variable number, what i dont understand is what number % 2 == 0 and number % number == 0 mean. I know that % is a remaining operator and == means equals, i know that = and == are not the same.

View Replies View Related

Billing Class - Properly Overloading Operators

May 4, 2014

I was told to create a class named Billing that includes three overloaded computeBill methods for a photobook store.

The first method takes the price of the book ordered, adds 8% tax and returns the total due.
The second method takes the price of the book, and the quantity, adds tax and returns the total.
The final method takes the price, quantity and a coupon discount, adds tax and returns the total.

All of this I have managed fairly well, although I keep getting a strange error at the end of my program that the constructor is undefined. The problem bits of code(the one throwing the errors) are under the comment //Enter values into each of the methods

Code:

package org.CIS406.lab2;

public class Billing {
//Declarations
double bookPrice;
int quantityOrdered;
double couponValue;

[Code] ....

My first thought was to create a constructor for each of the methods that I am using...

View Replies View Related

Swing/AWT/SWT :: Automatic Adjustment To Suit Size Of Operators Monitor

Feb 12, 2014

I'm building an application in Netbeans and so far I'm making good progress with it. I have created an animated splash screen in myspacegen.com and then downloaded it as a GUI file. I've then imported it into my application in Netbeans but I was restricted in myspacegens.com to the size of the image I could create. I want to know if it is possible to add code to the splash screen project within Netbeans so that it automatically adjusts to fill the screen. I have used the java commands get width and get height elsewhere in my application so that the window I create in a project automatically adjusts to fill the operator's screen but that has a 'window' to adjust and I do not want my splash screen to have a visible window.

View Replies View Related

Java Random Road Cross - Generate Random Number Between One To Ten

Dec 8, 2014

The program I'm supposed to create generates a random number between one to ten. Then the program is to ask me if I wish to cross the road. If you choose to cross, the outcomes for 0-2 are "You crossed safely." For 3-5, 75% of the time it should say "RIP you got run over", and 35% of the time it should say "You crossed the street." For 6-8, 60% of the time it should say you made it.", and 40% of the time it should say "You died". For 9-10, it should say "RIP".

So far I have gotten the random number generation part working. I have up to here:

import java.util.Random;
public class test4 {
public static void main(String[] args) {
Random random = new Random();
for(int i =0; i < 1; i++){
int num = random.nextInt(10) + 1;
System.out.println("The number of cars on the street are: " + num + "Do you wish to cross the road?");
}
}
}

View Replies View Related

Calculating Sin(x) Without Using Math

Sep 14, 2014

I'm trying to calculate sin(x) without using Math.sin(x). The formula for sin(x) is: x - x^3/3! + x^5/5! ... I can't seem to get the coding for the alternating +/- right. Here's my program:

import java.util.Scanner;
import java.lang.Math;
class Sin
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int n, c, fact = 1, count = 1;
double x, sum = 0, sum_sin = 0, result;

[Code] ....

View Replies View Related

Split A Math Expression?

Feb 27, 2015

I have a math expression in a String, that I want to split into 4 pieces as in the following example:

String math = "23+4=27"
int num1 = (23 STORES HERE)
int num2 = (4 STORES HERE)
String operator = (+ STORES HERE)
int answer = (27 STORES HERE)

Note that the operator can be either + - * /

View Replies View Related

Using MATH Class For Assignment?

Sep 18, 2014

Here is the assignment:

Prompt the user to input two sides (a and b) of a right triangle. Calculate, using the

formulas below, the hypotenuse and the other two angles of the triangle. Calculate the

perimeter and the area.

Here are the formulas:

a^2 + b^2 = h^2

sinθ = a/h

A + B + C = 180 degrees

how to start this program out, i know which math classes i need to use.

View Replies View Related

String To Math Operation

Sep 14, 2014

Given a string of numbers and operator. Compute as follows:

Consider, "000293000002030403+0400293059694" is the input.

a. Separate the two operands and one operator:
"000293000002030403" "+" "0400293059694"

b. On each string operand, take each digit. ADD them all.
(2+9+3+2+3+4+3) + (4+2+9+3+5+9+6+9+4)
The two string operands should now become two integers.

c. Lastly, perform the operation using the string operator specified on the original string.
26 + 51 = 77

I don't know why I get the sum of 52 when I use a for loop.

Here are my codes:

public class string2math {
public static void main(String[] args) {
String input = "000293000002030403+0400293059694";
String [] operand = input.split("+");
String firstOpera = operand[0];

[Code] ...

View Replies View Related

Use Math Methods Without Import In IDE

Feb 22, 2014

package statics;
public class Test_main {
public static void main(String[] args) {
Math.log(50.00);
}
}

I am able to use a Math method without importing the java.lang.Math. Which makes me think

1) What is the use of the import statement in java?
2) Is my IDE taking care of the import automatically?

I am using Eclipse IDE and JDK 1.7.

View Replies View Related

Outputs Not Displaying Correct Math

Oct 14, 2014

I'm having extreme troubles with my outputs not displaying the correct math. I have everything organized how I want it, it's just not giving me the correct answers.

The code is supposed prompt the user to enter an investment amount and an interest rate and display the future investment amount for years 1-30. The formula for this is:

futureInvestmentAmount = investmentAmount * (1 + monthlyInterestRate)^(numberOfYears*12)

Here is my code:

import java.util.Scanner;
public class InvestmentValue {
public static void main(String[] args) {
  // prompt user to enter data
Scanner scan = new Scanner(System.in);
System.out.println("The amount invested: ");

[Code] ....

And I have to use a method to do this as it is what we are learning in class right now. A sample output as of now is:

The amount invested:
1000
Annual interest rate:
9
Years Future Value
11.0E15
21.0E27

[Code] .....

And obviously a future investment amount cannot equal infinity.

View Replies View Related

Math Integration Across Multiple Arrays

Feb 14, 2014

I am trying to complete an assignment and I am stuck on a key point. I must print the total inventory value across each of these arrays, as it prints for each "list". To do this, I understand that I need some form of correlation between my variables "total" and "Price" between each array. My quandary lies in the way that I have designed this code to begin with. I am not sure where to add the math section of code that is needed, nor am I fluent enough with Java to know how to call all of the arrays as it prints. Listed below is my code, when you compile it the total inventory value is 0 for each "list".

package inventory;
public class Inventory {
public static void
main (String[] args) {
List[]list = new List[5];

[code]...

View Replies View Related

Building Multiple Math Games?

Nov 23, 2014

building a game. the game is all about the multiple times table with levels. easy medium and difficulty. I dont even know where to begin and what is the codes to use or even the platform. what websites can be access etc and what is the big deal with code tags.

View Replies View Related

Converting Math Formula To Java?

Jan 20, 2015

I'm trying to convert a Formula to run in a java program.

This is the formula:

(you can ignore the modifier part)

This is my attempt. However it prints out 0.0

import java.util.Scanner;
public class DamageCalc{
public static void main(String[] args){
Scanner s = new Scanner(System.in);

[Code].....

View Replies View Related

Importing Math Formula To Java

Sep 1, 2014

i am trying to calculate the number of months it takes to pay off a set amount of credit card debt given the following information: principle amount, annual interest rate, and monthly payment. How to import the formula necessary to system.out.print the answer (number of months it takes to pay off debt) ???

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved