Jump to content

Java Loop Help

creativename
Go to solution Solved by x7ASSASSIN7x,
import java.util.Scanner;
 
public class Name {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
Scanner keyboard1 = new Scanner(System.in);
String name;
int i;
 
 
System.out.println("How many times would you like to print your name?");
i = keyboard.nextInt();
int m = i;
System.out.println("What is your name?");
name = keyboard1.nextLine();
do
{
System.out.println(name);
m--;
}while(m!=0);
 
 
}
 
}

Switch the statements to the way you would like them to look

I'm trying to make a simple program that does these things

 

1. Ask for the user's name

2. Ask's how many times he/she wants it to appear

3. Loop the user's name that many times as the output.

 

 

Example of the output I want

 

What is your name?

John

How many times would you like it to loop?

3

John

John

John

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

Print the question.

Store the input in a string (Use the scanner class to read).

Print the second question.

Store the input in an integer variable.

Use a for loop taking advantage of the aforementioned variable as your loop control variable.

TrinityFX


CPU: i5-4440 @ 3.1Ghz RAM: Crucial 1 x 4GB @ 1.6Ghz  MOTHERBOARD: MSI Z87-G41 PC Mate GPU: PCS+ HD 7870 Ghz Edition | CASE: NZXT Source 210 Window | HDD #1: Toshiba 3.5" 500GB HDD #2: Toshiba 2.5" 500GB PSU: Cougar SL500 OS: Windows 8.1 Pro 


 | https://pcpartpicker.com/b/2wccCJ |

Link to comment
Share on other sites

Link to post
Share on other sites

import java.util.Scanner;
 
public class Name {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
Scanner keyboard1 = new Scanner(System.in);
String name;
int i;
 
 
System.out.println("How many times would you like to print your name?");
i = keyboard.nextInt();
int m = i;
System.out.println("What is your name?");
name = keyboard1.nextLine();
do
{
System.out.println(name);
m--;
}while(m!=0);
 
 
}
 
}

Switch the statements to the way you would like them to look

CPU: i5 25000 3.3 GHz             Motherboard: Asus P8Z77-V-LK          Ram: Corsair Vengeance 8 GB           Display: Samsung SD24 24" 1080p          

GPU: EVGA GTX 660 SC 2 GB     Sound: Razer Kraken pro                            PSU: Rosewill 630 watt                    Storage 1: 128 GB Adata XPG SX900 SSD

Case: Rosewill Challenger       CPU Cooler: Corsair h80i                    Mouse: Corsair M65                                   Storage 2: Seagate 2 TB HDD

Link to comment
Share on other sites

Link to post
Share on other sites

// TODO Auto-generated method stub

Scanner keyboard = new Scanner(System.in);
Scanner keyboard1 = new Scanner(System.in);

There's really no need to declare two Scanner objects.

TrinityFX


CPU: i5-4440 @ 3.1Ghz RAM: Crucial 1 x 4GB @ 1.6Ghz  MOTHERBOARD: MSI Z87-G41 PC Mate GPU: PCS+ HD 7870 Ghz Edition | CASE: NZXT Source 210 Window | HDD #1: Toshiba 3.5" 500GB HDD #2: Toshiba 2.5" 500GB PSU: Cougar SL500 OS: Windows 8.1 Pro 


 | https://pcpartpicker.com/b/2wccCJ |

Link to comment
Share on other sites

Link to post
Share on other sites

Use a for statement... and not we don't do homework so post your code please.

It's not homework.

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

There's really no need to declare two Scanner objects.

Did it quick :P your right though

CPU: i5 25000 3.3 GHz             Motherboard: Asus P8Z77-V-LK          Ram: Corsair Vengeance 8 GB           Display: Samsung SD24 24" 1080p          

GPU: EVGA GTX 660 SC 2 GB     Sound: Razer Kraken pro                            PSU: Rosewill 630 watt                    Storage 1: 128 GB Adata XPG SX900 SSD

Case: Rosewill Challenger       CPU Cooler: Corsair h80i                    Mouse: Corsair M65                                   Storage 2: Seagate 2 TB HDD

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×