-6

Java homework

I'm new to Java so I need a little help. For example, the customer needs to enter a positive number, e.g. 5. So I need to print number 5 five times, like 5,5,5,5,5 using only loops. i tried this

import java.util.Scanner;

public class Zadatak1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter one positive number:");
    int number = sc.nextInt();

    while ( number > 0) {
        System.out.println(number);
        number--;
    }



    sc.close();

IF a user enters number 4(that why there is a scanner) the program will print number 4 FOUR TIMES,just like this: 4 4 4 4 and idk how to write that program,the code i wrote dont work like that.

Mire
  • 1
  • 5
  • 2
    Have a look at this tutorial: [The `for` Statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) – Jesper Nov 12 '19 at 07:58
  • I deadass dont know how to write this code.I tried with while loop but i cant make it write one number many times equal to his value. – Mire Nov 12 '19 at 08:03
  • 2
    Use variables. Store the value you want to print in a variable and just print that. There's no rule saying you must use the index for your while loop or for loop to print your value. – John Kim Nov 12 '19 at 08:08
  • Does this answer your question? [How does the Java 'for each' loop work?](https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) – U880D Nov 12 '19 at 08:14
  • 1
    The problem with your code is that every iteration of the while loop modifies the value being displayed. Here's a hint: use a variable for counting number of loop iterations and another variable for condition of the loop – CR0N0S.LXIII Nov 12 '19 at 08:28

2 Answers2

1

Let's analyse what your code does:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter one positive number:");
    int number = sc.nextInt();

The first part is ok, it just waits for an input number. Optional: You could do some error checking to make sure that you handle what happens in case sc.nextInt() throws an exception.

This is where the problem is. Let's see what this code does exactly:

    while ( number > 0) { // repeat the actions in the following block as long as number>0
        System.out.println(number); // print number
        number--; // decrease number by one
    }

It seems obvious in these terms that if you input 5, the output is going to be 5,4,3,2,1. You need to make sure that you don't modify the value you need to display!

I will not spoonfeed you the correct code, but you can make questions in the comments and I will be happy to give you more hints :)

sephiroth
  • 896
  • 12
  • 22
-2

try this

public class Zadatak1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter one positive number:");

int number = sc.nextInt();

int count=0;

while ( count< number) {

// if you want to print the number in a separate line use this

-> System.out.println(number);

// if you want format like 4,4,4,4 use this

->System.out.print(number+",");

    count++;
}

sc.close();