-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
  • 3
  • 1
    What have you tried so far? – CR0N0S.LXIII Nov 12 '19 at 07:56
  • 1
    what have you tried so far? – Scary Wombat Nov 12 '19 at 07:56
  • Pretty cool huh, monkeys and Shakespeare and all that stuff – Scary Wombat Nov 12 '19 at 07:57
  • 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
  • 4
    Look at the top two comments, show the code – Scary Wombat Nov 12 '19 at 08:04
  • 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
  • look man this is the code i tried 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(broj); number--; } sc.close(); } } nut i need it to print number user enters that many times so if he enters 3 to print it like 3,3,3 – Mire Nov 12 '19 at 08:11
  • 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
    Post the code editing the answer, not in the comments – CR0N0S.LXIII Nov 12 '19 at 08:20
  • 2
    Modify your question instead of posting code in comments. We understand what you're trying to do, we're just not giving you the answer because it's a homework question which you need to solve on your own. Not to mention, you already got an answer from someone that you can use (which he shouldn't have given in the first place IMO), all you need to do is put two and two together to take input from the user. – John Kim Nov 12 '19 at 08:20
  • Yeah ik sorry for posting in comments,im kinda in hurry. – Mire Nov 12 '19 at 08:21
  • If you are in a hurry, you will want to do the best you can to help us help you. You will get help faster that way. So do edit the question. – Ole V.V. Nov 12 '19 at 08:26
  • 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
  • 876
  • 11
  • 22
-1

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();