Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

java - System.out.println issue

import java.util.*;

    public class Factors {
        public static void main (String [] args){
            System.out.println("Enter a number to find the factors for: ");
            Scanner keyboard = new Scanner(System.in);
            int input = keyboard.nextInt();
            ArrayList<Integer> Factors = new ArrayList<Integer>();
            for(int i = 1; i <= input; i++){
                if(input % i == 0){
                    if(Factors.contains(i)){
                        break;
                    }
                    Factors.add(i);
                    Factors.add(input / i);
                    System.out.println("Factors are: " + i + " and " + input / i);
                }
            }
        }
    }

The second if statement is simply so it doesn't repeat factors. But I want it to print "Enter a number to find the factors for: and then read a user input on the same line. If I use println, then it goes to the next line. If I use print, then it won't ask "Enter a number to find the factors for: until I give an input. How can I fix this and why does it happen?

question from:https://stackoverflow.com/questions/65910860/system-out-println-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...