将字符串连接到数组列表中
Concatenating strings into array list
您好,我正在尝试扫描数字 1-113 并执行以下操作
我。如果数字是奇数,打印“x是奇数”
二.如果数字可以被 5 整除,打印“hi five”
三。如果一个数字 (x) 及其后续数字 (x+1) 的总和是一个可以被 7 整除的值,打印“wow”
四.如果数字是质数,则打印“质数”。
否则只打印数字
这些语句中的每一个都应该用逗号分隔。这些功能必须保持原样以匹配提供的 API,现在的问题是我当前的代码在不同的行上显示所有内容
1为奇数,
总理
2,
3为奇数,
总理
哇
什么时候应该这样显示
1 是奇数,素数,
2,
3是奇数,素数,哇
如果有任何关于如何进行的建议,我将不胜感激。
import java.util.ArrayList;
public class Number
{
//check if by number is prime
public static boolean isPrime(int n){
//check if n is a multiple of 2
if (n%2==0) return false;
//if not, then just check the odds
for(int i=3;i*i<=n;i+=2) {
if(n%i==0)
return false;
}
return true;
}
//check if number is odd
public static boolean isOdd(int n){
if(n % 2 == 0){
return false;
}
return true;
}
//checks if number is divisible by 5
public static boolean isDivisibleBy5(int n){
if(n % 5 == 0){
return true;
}
return false;
}
//checks if number is divisible by 7
public static boolean isDivisibleBy7(int n){
if(n % 7 == 0){
return true;
}
return false;
}
//Each number from 1 to 113 is checked and if
public static ArrayList<String> iterate(){
//initialize arraylist of strings
ArrayList<String> al = new ArrayList<String>();
//for each number between 1 and 113 do the following
for(int i=1; i< 113; i++){
if (isOdd(i) == true){
al.add(i+" is odd, ");
}
else{
al.add(i+", ");
}
if (isDivisibleBy5(i) == true){
al.add(" high 5, ");
}
if(isPrime(i) == true){
al.add("Prime");
}
if(isDivisibleBy7(i+(i+1))==true ){
al.add("wow");
}
}
return al;
}
public static void main(String[] args) {
ArrayList<String> numbers;
numbers = iterate();
for(int i=0; i < numbers.size(); i++){
System.out.println(numbers.get(i));
}
}
}
您正在使用 System.out.println
,您需要使用 System.out.print
。
因此,如果您试图描述一个具有多个属性的数字,那您就做错了。
list.add(i+"something");
list.add("Prime");
创造
{"isomething", "Prime"}
当你想要的时候
{"isomething,Prime"}
但您所做的只是每次调用 .add
时将列表增加 1 个元素
尝试更改为
for(int i=1; i< 113; i++){
String numberString = "" + i;
if (isOdd(i) == true){
numberString += ", is odd";
}
else{
numberString += ",";
}
if (isDivisibleBy5(i) == true){
numberString += ", is even"
}
if(isPrime(i) == true){
numberString += "Prime";
}
if(isDivisibleBy7(i+(i+1))==true ){
numberString += "wow";
}
al.add(numberString);
}
您好,我正在尝试扫描数字 1-113 并执行以下操作
我。如果数字是奇数,打印“x是奇数”
二.如果数字可以被 5 整除,打印“hi five”
三。如果一个数字 (x) 及其后续数字 (x+1) 的总和是一个可以被 7 整除的值,打印“wow”
四.如果数字是质数,则打印“质数”。
否则只打印数字
这些语句中的每一个都应该用逗号分隔。这些功能必须保持原样以匹配提供的 API,现在的问题是我当前的代码在不同的行上显示所有内容
1为奇数,
总理
2,
3为奇数,
总理
哇
什么时候应该这样显示
1 是奇数,素数,
2,
3是奇数,素数,哇
如果有任何关于如何进行的建议,我将不胜感激。
import java.util.ArrayList;
public class Number
{
//check if by number is prime
public static boolean isPrime(int n){
//check if n is a multiple of 2
if (n%2==0) return false;
//if not, then just check the odds
for(int i=3;i*i<=n;i+=2) {
if(n%i==0)
return false;
}
return true;
}
//check if number is odd
public static boolean isOdd(int n){
if(n % 2 == 0){
return false;
}
return true;
}
//checks if number is divisible by 5
public static boolean isDivisibleBy5(int n){
if(n % 5 == 0){
return true;
}
return false;
}
//checks if number is divisible by 7
public static boolean isDivisibleBy7(int n){
if(n % 7 == 0){
return true;
}
return false;
}
//Each number from 1 to 113 is checked and if
public static ArrayList<String> iterate(){
//initialize arraylist of strings
ArrayList<String> al = new ArrayList<String>();
//for each number between 1 and 113 do the following
for(int i=1; i< 113; i++){
if (isOdd(i) == true){
al.add(i+" is odd, ");
}
else{
al.add(i+", ");
}
if (isDivisibleBy5(i) == true){
al.add(" high 5, ");
}
if(isPrime(i) == true){
al.add("Prime");
}
if(isDivisibleBy7(i+(i+1))==true ){
al.add("wow");
}
}
return al;
}
public static void main(String[] args) {
ArrayList<String> numbers;
numbers = iterate();
for(int i=0; i < numbers.size(); i++){
System.out.println(numbers.get(i));
}
}
}
您正在使用 System.out.println
,您需要使用 System.out.print
。
因此,如果您试图描述一个具有多个属性的数字,那您就做错了。
list.add(i+"something");
list.add("Prime");
创造
{"isomething", "Prime"}
当你想要的时候
{"isomething,Prime"}
但您所做的只是每次调用 .add
时将列表增加 1 个元素尝试更改为
for(int i=1; i< 113; i++){
String numberString = "" + i;
if (isOdd(i) == true){
numberString += ", is odd";
}
else{
numberString += ",";
}
if (isDivisibleBy5(i) == true){
numberString += ", is even"
}
if(isPrime(i) == true){
numberString += "Prime";
}
if(isDivisibleBy7(i+(i+1))==true ){
numberString += "wow";
}
al.add(numberString);
}