[英]Separate odd even from string
The below is the question:下面是问题:
Get comma separated String of numbers from user and print the set of odd numbers and even numbers.从用户获取逗号分隔的数字字符串并打印奇数和偶数的集合。
sample input:样本输入:
1,2,3,4,5,6,7,8,9,10 1、2、3、4、5、6、7、8、9、10
Sample output:示例输出:
Odd Numbers:奇数:
1,3,5,7,9 1、3、5、7、9
Even Numbers:偶数:
2,4,6,8,10 2、4、6、8、10
Sample input:样本输入:
20,30,40 20、30、40
Sample output:示例输出:
Even Numbers:偶数:
20,30,40 20、30、40
My code:我的代码:
class OddEv{
public static void main(String args[]){
String s;
Scanner in=new Scanner(System.in);
s=in.nextLine();
for(int i=0;i<s.length();i++){
if(s.charAt(i)%2==0){
System.out.print(s.charAt(i));
}
if(s.charAt(i)%2!=0){
System.out.print(s.charAt(i));
}
}
but I am not getting the right answer.但我没有得到正确的答案。 What changes should I bring to get the right output according to the question根据问题,我应该进行哪些更改才能获得正确的输出
actually I don't know java very well so I don't know what to do here实际上我不太了解java所以我不知道在这里做什么
You can try this simple solution, without using any other concepts like regex
.您可以尝试这个简单的解决方案,而无需使用任何其他概念,例如regex
。 For this, you can split your string and store it in a string array, and than iterating over an array you can check whether the number is odd or even.为此,您可以拆分字符串并将其存储在字符串数组中,然后遍历数组,您可以检查数字是奇数还是偶数。 Following code will store all the even and odd numbers from your string into the array named even
and odd
.以下代码会将字符串中的所有偶数和奇数存储到名为even
和odd
的数组中。
String s = "1,2,3,4,5,6,7,8,9,10";
int even[] = new int[10];
int odd[] = new int[10];
String ar[] = s.split(",");
int j=0,k=0,oddChecker=0,evenChecker=0;
for(int i=0;i<ar.length;i++){
if(Integer.parseInt(ar[i])%2 == 0){
even[j] = Integer.parseInt(ar[i]);
++j;
evenChecker = 1;
}
else{
odd[k] = Integer.parseInt(ar[i]);
++k;
oddChecker = 1;
}
}
if(oddChecker == 0){
System.out.println("even");
System.exit(0);
}
if(evenChecker == 0){
System.out.println("odd");
System.exit(0);
}
System.out.println("Even numbers:");
for(int i=0;i<j;i++){
if(i!=j-1){
System.out.print(even[i]+",");
}
else{
System.out.print(even[i]);
}
}
System.out.println();
System.out.println("Odd numbers:");
for(int i=0;i<k;i++){
if(i!=k-1){
System.out.print(odd[i]+",");
}
else{
System.out.print(odd[i]);
}
}
Output:输出:
Even numbers:偶数:
2,4,6,8,10 2、4、6、8、10Odd numbers:奇数:
1,3,5,7,9 1、3、5、7、9
Don't forget to convert String to Integer when checking the condition and adding numbers to arrays.在检查条件并将数字添加到数组时,不要忘记将 String 转换为 Integer。 For that I've used Integer.parseInt(your_string)
.为此,我使用了Integer.parseInt(your_string)
。
use two ArrayList
of Integer
for odds and evens, and change String s
to String []s
then use userInput.split(",")
to separate numbers.使用两个ArrayList
的Integer
表示奇数和偶数,并将String s
更改为String []s
然后使用userInput.split(",")
分隔数字。 parse strings in s to integer using Integer.parseInt(str)
method then use if statement to determine number is odd or even and add them to arraylists.使用Integer.parseInt(str)
方法将 s 中的字符串解析为整数,然后使用 if 语句确定数字是奇数还是偶数,并将它们添加到数组列表中。
public static void main(String args[]) {
String s[];
Scanner in = new Scanner(System.in);
s = in.nextLine().split(",");
ArrayList<Integer> odds = new ArrayList<>();
ArrayList<Integer> evens = new ArrayList<>();
for (String item : s) {
int number = Integer.parseInt(item);
if (number % 2 == 0) {
evens.add(number);
} else {
odds.add(number);
}
}
}
System.out.println("Even Numbers:"); System.out.println(Arrays.toString(evens.toArray()).replaceAll("[\\\\p{Ps}\\\\p{Pe}]", "")); System.out.println("Odd Numbers:"); System.out.println(Arrays.toString(odds.toArray()).replaceAll("[\\\\p{Ps}\\\\p{Pe}]", ""));
10,11,12,13,14,15,16,17,18,19,20
sample output:示例输出:
Even Numbers: 10, 12, 14, 16, 18, 20 Odd Numbers: 11, 13, 15, 17, 19
hope this solves your problem希望这能解决你的问题
public class CommaSeperatedNum {
public static void main(String args[]){
String s;
Scanner in=new Scanner(System.in);
s=in.nextLine();
String numarray[] = s.split(",");
int odd[] = new int[20];
int oddcnt=0;
int even[] = new int[20];
int evencnt =0;
for(int i=0;i<numarray.length;i++)
{
if( Integer.parseInt((numarray[i]).trim())%2 ==0){
odd[oddcnt] = Integer.parseInt(numarray[i].trim());
oddcnt++;
}
{
even[evencnt] = Integer.parseInt(numarray[i].trim());
evencnt++;
}
}
System.out.print("Odd Numbers : " );
for (int i = 0; i < odd.length && odd[i] != 0; i++) {
System.out.print(odd[i]+" " );
}
System.out.println();
System.out.print("Even Numbers : " );
for (int i = 0; i < even.length && even[i] != 0; i++) {
System.out.print(even[i]+" " );
}
}
Following is a more functional solution with less moving parts:以下是一个功能更强大的解决方案,移动部件更少:
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(",");
String evens = Arrays.stream(split)
.filter(number -> Integer.parseInt(number) % 2 == 0)
.collect(Collectors.joining(","));
String odds = Arrays.stream(split)
.filter(number -> Integer.parseInt(number) % 2 != 0)
.collect(Collectors.joining(","));
System.out.println("Even Numbers:\n" + evens);
System.out.println("Odd Numbers:\n" + odds);
}
Yeah, it's quite inefficient, I know.是的,它非常低效,我知道。 There's probably a better way to do it, but I just wanted the OP to get a hint of how descriptive and simple a code can be.可能有更好的方法来做到这一点,但我只是希望 OP 能够了解代码的描述性和简单性。
a lambda with partitioningBy
does the separation带有partitioningBy
的 lambda 进行分离
it depends only on the last digit whether a string represents an even or odd number它只取决于最后一位数字是表示偶数还是奇数
a conversion into a numerical value is not necessary不需要转换为数值
String inp = "1,2,3,4,5,6,7,8,9,10";
String[] nums = inp.split( "," );
Map<Boolean, List<String>> map = Arrays.stream( nums ).collect(
partitioningBy(s -> "02468".indexOf(s.charAt(s.length() - 1)) >= 0));
System.err.println( "even: " + map.get( true ) );
System.err.println( "odd: " + map.get( false ) );
even: [2, 4, 6, 8, 10]
odd: [1, 3, 5, 7, 9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.