Jump to content

Java Loop Help?

Speedyv

Put the whole thing in a while loop?

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Put the whole thing in a while loop?

 

I want to loop it until it gets 4 pokemon.

System: Thinkpad T460

 

Link to comment
Share on other sites

Link to post
Share on other sites

I want to loop it until it gets 4 pokemon.

int pokemon = 0; 

while (pokemon < 4){

your code

pokemon++;

}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

import java.util.Scanner;public class  pokemon{      public static void main(String[] args)    {        Scanner s = new Scanner(System.in);        System.out.print("Welcome ");        System.out.print( "Enter your name: "  );        String name = s.nextLine();        System.out.println( "Hello " + name + "!" );        System.out.print("Please Enter Pokemon Name and Level: " );         s.useDelimiter(" ");	String pokemon = s.next();        int level = s.nextInt();	for(int i = 0; i < 4; i++)	{		if  ((pokemon.equalsIgnoreCase("charmander") || pokemon.equalsIgnoreCase("bulbasaur") || pokemon.equalsIgnoreCase("squirtle")) && level < 16)                { 			System.out.println("Added Level " + level + " " + pokemon);                 }                    else if (pokemon.equalsIgnoreCase("charmander") || pokemon.equalsIgnoreCase("bulbasaur") || pokemon.equalsIgnoreCase("squirtle") && level >= 16)                {		        System.out.println("Invalid level!");                }                 else 	        { 			System.out.println("Invalid Pokemon! Invalid level!");                }	        pokemon = s.next();	        level = s.nextInt();        }    }} 

I modified it a bit you had a redundant line of code and your better of using a for loop instead. I also separated .useDelimiter() and .next() IMO looks cleaner.

Main PC: CPU: i7-4770k RAM: 16GB Kingston HyperX Blu SSD: Samsung 850 Pro 256GB HDD: 1TB WD Blue GPU: ASUS GeForce GTX 770 2GB PSU: Corsair CX600M Case: Bitfenix Shinobi OS: Windows 10 Pro 64-Bit

 

Laptop: ASUS N56VJ

Link to comment
Share on other sites

Link to post
Share on other sites

for(int pokemon = 0; pokemon <= 4; pokemon++)

{

      ...............code.............

}

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

×