The program should output all numbers between the starting and ending number that are a multiple of the number N. Your solution must use a for-loop. Here is example output from running the program:
import java.util.Scanner;
public class MultiplyNThree {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner userInputScanner = new Scanner(System.in);
System.out.println("Enter 1st number: ");
int start = userInputScanner.nextInt();
import java.util.Scanner; public class Practice { public static void main(String[] args) { //get user input Scanner user_input = new Scanner(System.in); System.out.print("Enter a number: " );
Ok, so I've seen alot of things online about writing switch code, but I can't find any involving user input, let alone multiple input scanners.
The goal is I let the user put in two different integers and it will decide based on both of the integers which of the 15 possible results it will display.
I am also not allowed to use if/else statements in this program.
Alright so I wrote a switch statement that decides what range to print based on the letter grade input.
import java.util.Scanner; public class SwitchPractice { public static void main(String [] args) { Scanner scan = new Scanner(System.in);
[code]...
It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h My guess is too high?
My fifth guess is 16: h *** I am unlucky this round. I give up.
Let's play!
My first guess is 10: etc..
import java.util.*; import java.lang.Math; public class numguessprac1 { // Declaring variables public static String input; public static int quit; public static int guess; public static int wins;
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber { Scanner scan = new Scanner(System.in); int userNum; String neg;
class Client{ public static void main(String []args){ Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params Bike R2=new Bike();//without params System.out.println(R1.increaseSpeed());//calling methods System.out.println(R1.maxDistance()); System.out.println(R2.increaseSpeed()); System.out.println(R2.maxDistance()); } }
I'm working on an assignment where I need to take radius and height from the user to use with the methods in class Cylinder. I have to use radius and height as input parameters in the methods that calculate: Base area, lateral area, total area, and volume. But when I use height and radius as input parameters then it just prints zeros for all calculated values. When I remove (double radius) from the method it works just fine. So my question is how can I get this to work with radius/height as input parameters? Or am I just misunderstanding will they still be input parameters even if I don't have it written as (double radius) in the method?
I need to generate a league table in Java based on results provided in the shape of user input. I have the 6 teams in an array. I need to ask the user how many wins, draws, losses each team has had.
for (String element : teams) { System.out.println( "Please enter the number of wins, draws and losses for " + element); int[] wins = new int [6]; int[] draws = new int [6]; int[] losses = new int [6]; }
How do I pull 3 separate values from the user and then store each value in a separate element of each array? so if one team wins 3, draws 2 and loses 1 - I need to put 3 into the wins array, 2 into the draws array and 1 into the losses array? I then need to total all this in a table.
I am trying to create a Community Theater program that has Actors, Directors, Investors, Staff etc. I should eventually be able to input the information for the people from both a file and the keyboard. I'm working on trying to take in multiple Actors from one file and adding them to an ArrayList of Actors. My code takes in only one actor right now.
I am trying to take in multiple sets of data for the same type of class but creating new instances after each set of data (16 lines). Each actor has 16 lines of data (text file included), so I was thinking that I would need perhaps a for loop but I am unsure of how and where to implement it. I am also unsure about adding these new actors to the Actor ArrayList. I'm a little shaky on the idea of creating multiple objects when it is unknown how many you need to create. Right now, I know how many Actors I need to add but it would be nice if I could learn how to expand that based on the user's needs.
My Questions: How can I write my program to know when it is done reading 16 lines and then initialize a new Actor (will expand to other people)?(expanded below)Eventually, I plan on using my theaterObj (an instance of CommunityTheater class), what if I wanted to move the readActorFile method to that class? Would that change the way I implement anything majorly? Would it still be a static method? I ask this because I want the Actors array to be a variable in the CommunityTheater class. I would extend this to adding actors to an ArrayList in a Play class which would be in CommunityTheater as well. So, essentially a CommunityTheater has different Play(s), and each Play contains a list of Actors.
I don't exactly know where I should put a loop (should I use a loop?) to read through each line, in readActorFile or in readInFile? How should I write this loop, as in, how do tell my program what line it's reading?
for(countLine=0; countLine < 17; countLine++) or while(countLine<17) read all the info and then an if(?)-
if(file has a next line)-I don't really know how to code this-start over with loop of reading information?I don't know about the for loop, as I'm not sure how to "count" lines in a text file.OR I've been thinking that readInFile in the Actor class could return something(true/false?) to let the method readInActor know that it's done reading, then readInActor can check to see if there's anything else in the file, if there is- it would call the readInFile in Actor class again to read in information... and so on. But again I am unsure of how to code this.
I'm trying to build a program that will output what will ultimately look like a simple mario level turned on its side. As part of my output I need the user to define what mario looks like. I do this using Scanner and save the input to String mario. When I try to use that variable in another method it gives me troubles.
import java.util.Scanner; public class Mario2 { public static void mario() { //user defines mario String mario = ">->O"; Scanner keys = new Scanner(System.in); System.out.println("What does mario look like?"); mario = keys.next(); System.out.println("Mario now looks like: " + mario);
The information is getting stored in the session and i am able to retrieve the same for single user. The trouble starts when 2 user logged in from different browser (for example chrome, IE) from the same machine. The 2nd logged in user, overwrite the session information of first user.
How to prevent this.
My environment: Windows 7 64 bit, IBM websphere server, Oracle 11g, JSP, Sevrlet.
I have been coding in my class at school (Grade 11 Computer science) and i just downloaded the program on my computer at home, unfortunately i cannot access my computer notes at home and i also dont remember certian specifics of coding, so my question is how would i get user input to create a program. The comments are the parts i dont remember. (I am trying to slowly build my memory with this stuff)
Here is my code so far:
import java.util; [highlight=java] public class hello_world { public static void main(String[] args) { string name; //WHAT DO I PUT HERE????
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object Scanner keys = new Scanner(System.in); //Get chips
[Code]....
*****This is what I get when I run it
run:
How much money would you like to change? 50
You now have $50 in chips.
What game do you want to play? You did not pick dice.
I have been given a piece of work to do for College that requires me to format user input. For example: This is an example of text that will have straight left and right margins after formatting
The user inputs the width, in the case above being 20. Each line has to therefore be 20 characters long. All spaces are to be replaced with full stops.
This.is.an.example.o f.text.that.will.hav e.straight.left.and. right.margins.after. formatting.......... public class Main { public static void main ( String args[])
ArrayList<Integer> leftCU = new ArrayList<>(); { System.out.println ("Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done."); leftCU.add(in.nextInt());
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object Scanner keys = new Scanner(System.in); //Get chips System.out.print("How much money would you like to change? "); int chips = keys.nextInt(); System.out.println("You now have $" + chips + " in chips.");
[code]...
This is what I get when I run it run/How much money would you like to change?
I created a scanner object but it's not asking for any input. when I create a new scanner and then tell it to print it it just prints a bunch of weirdness when AFIK it should be asking me to type something and then it should repeat what i entered.
I trying to get this code to get user input instead of reading from a hardcoded array. I'm getting compile errors while trying to get user input. Here's some of the code: