Java 从字符串输入打印偶数字?
我正在学习一门初级课程,但对于以下问题的解决方法有困难:编写一个程序,要求用户输入一行输入。然后,程序应显示一行,其中仅包含偶数字。 例如,如果用户输入Java 从字符串输入打印偶数字?,java,string,Java,String,我正在学习一门初级课程,但对于以下问题的解决方法有困难:编写一个程序,要求用户输入一行输入。然后,程序应显示一行,其中仅包含偶数字。 例如,如果用户输入 I had a dream that Jake ate a blue frog, 输出应该是 had dream Jake a frog 我不知道用什么方法来解决这个问题。我从以下内容开始,但我知道这将简单地返回整个输入: import java.util.Scanner; public class HW2Q1 { public
I had a dream that Jake ate a blue frog,
输出应该是
had dream Jake a frog
我不知道用什么方法来解决这个问题。我从以下内容开始,但我知道这将简单地返回整个输入:
import java.util.Scanner;
public class HW2Q1
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a sentence");
String sentence = keyboard.next();
System.out.println();
System.out.println(sentence);
}
}
我不想透露这个问题的答案(为了考试,不在这里),但我建议你调查一下
String.Split()
从这里开始,您需要遍历结果,并合并到另一个字符串中进行输出。希望这能有所帮助。我不想透露问题的答案(为了考试,而不是在这里),但我建议你调查一下
String.Split()
从这里开始,您需要遍历结果,并合并到另一个字符串中进行输出。希望有帮助。您应该使用句子分割(regex)正则表达式将描述分隔世界的内容,在您的例子中是空格(“”),因此regex将如下所示:
regex="[ ]+";
[]
表示空格将分隔单词+
表示空格可以是单个或多个连续空格(即一个或多个空格)
您的代码可能如下所示
Scanner sc= new Scanner(System.in);
String line=sc.nextLine();
String[] chunks=line.split("[ ]+");
String finalresult="";
int l=chunks.length/2;
for(int i=0;i<=l;i++){
finalresult+=chunks[i*2]+" ";//means finalresult= finalresult+chunks[i*2]+" "
}
System.out.println(finalresult);
Scanner sc=新扫描仪(System.in);
字符串行=sc.nextLine();
String[]chunks=line.split([]+”);
字符串finalresult=“”;
int l=chunks.length/2;
对于(int i=0;i你应该使用句子。分割(regex)正则表达式将描述你的世界之间的区别,在你的例子中是空格(“”),所以regex将是这样的:
regex="[ ]+";
[]
表示空格将分隔单词+
表示空格可以是单个或多个连续空格(即一个或多个空格)
您的代码可能如下所示
Scanner sc= new Scanner(System.in);
String line=sc.nextLine();
String[] chunks=line.split("[ ]+");
String finalresult="";
int l=chunks.length/2;
for(int i=0;i<=l;i++){
finalresult+=chunks[i*2]+" ";//means finalresult= finalresult+chunks[i*2]+" "
}
System.out.println(finalresult);
Scanner sc=新扫描仪(System.in);
字符串行=sc.nextLine();
String[]chunks=line.split([]+”);
字符串finalresult=“”;
int l=chunks.length/2;
对于(int i=0;i虽然有更简单、更容易的方法,但我将使用基本结构-for loop
、if block
和While loop
来实现它。我希望您能够破解代码。尝试运行它,如果有错误,请告诉我
String newsent;
int i;
//declare these 2 variables
sentence.trim(); //this is important as our program runs on space
for(i=0;i<sentence.length;i++) //to skip the odd words
{
if(sentence.charAt(i)=" " && sentence.charAt(i+1)!=" ") //enters when a space is encountered after every odd word
{
i++;
while(i<sentence.length && sentence.charAt(i)!=" ") //adds the even word to the string newsent letter by letter unless a space is encountered
{
newsent=newsent + sentence.charAt(i);
i++;
}
newsent=newsent+" "; //add space at the end of even word added to the newsent
}
}
System.out.println(newsent.trim());
// removes the extra space at the end and prints newsent
stringnewsent;
int i;
//声明这两个变量
station.trim();//这一点很重要,因为我们的程序是在空间上运行的
对于(i=0;i虽然有更简单、更容易的方法,但我将使用基本结构-for loop
、if block
和While loop
来实现它。我希望您能够破解代码。试着运行它,如果有错误,请告诉我
String newsent;
int i;
//declare these 2 variables
sentence.trim(); //this is important as our program runs on space
for(i=0;i<sentence.length;i++) //to skip the odd words
{
if(sentence.charAt(i)=" " && sentence.charAt(i+1)!=" ") //enters when a space is encountered after every odd word
{
i++;
while(i<sentence.length && sentence.charAt(i)!=" ") //adds the even word to the string newsent letter by letter unless a space is encountered
{
newsent=newsent + sentence.charAt(i);
i++;
}
newsent=newsent+" "; //add space at the end of even word added to the newsent
}
}
System.out.println(newsent.trim());
// removes the extra space at the end and prints newsent
stringnewsent;
int i;
//声明这两个变量
station.trim();//这一点很重要,因为我们的程序是在空间上运行的
因为(i=0;i既然你说你是初学者,我就试着用简单的方法
您可以使用indexOf()方法查找空格的索引。然后,使用while循环计算句子的长度,将每个偶数词添加到句子中。要确定偶数词,请创建一个整数,并在while循环的每次迭代中向其添加1。使用(您创建的整数)%2==0以确定您是在偶数还是奇数迭代中。在每次偶数迭代中连接单词(使用if语句)
如果索引超出范围-1,则通过在末尾添加空格来操作输入字符串
记住,构造循环时,无论是偶数还是奇数迭代,计数器都会增加1
您也可以删除奇数单词,而不是将偶数单词串联起来,但这将更加困难。既然您说您是初学者,我将尝试使用简单的方法
您可以使用indexOf()方法查找空格的索引。然后,使用while循环计算句子的长度,将每个偶数词添加到句子中。要确定偶数词,请创建一个整数,并在while循环的每次迭代中向其添加1。使用(您创建的整数)%2==0以确定您是在偶数还是奇数迭代中。在每次偶数迭代中连接单词(使用if语句)
如果索引超出范围-1,则通过在末尾添加空格来操作输入字符串
记住,构造循环时,无论是偶数还是奇数迭代,计数器都会增加1
您也可以删除奇数字而不是连接偶数字,但这将更加困难。不确定如何处理字与字之间的多个空格或条目中奇怪的非字母字符,但这应该考虑到主要用例:
import java.util.Scanner;
public class HW2Q1 {
public static void main(String[] args)
{
System.out.println("Enter a sentence");
// get input and convert it to a list
Scanner keyboard = new Scanner(System.in);
String sentence = keyboard.nextLine();
String[] sentenceList = sentence.split(" ");
// iterate through the list and write elements with odd indices to a String
String returnVal = new String();
for (int i = 1; i < sentenceList.length; i+=2) {
returnVal += sentenceList[i] + " ";
}
// print the string to the console, and remove trailing whitespace.
System.out.println(returnVal.trim());
}
}
import java.util.Scanner;
公共类HW2Q1{
公共静态void main(字符串[]args)
{
System.out.println(“输入句子”);
//获取输入并将其转换为列表
扫描仪键盘=新扫描仪(System.in);
字符串语句=keyboard.nextLine();
字符串[]句子列表=句子。拆分(“”);
//遍历列表并将具有奇数索引的元素写入字符串
String returnVal=新字符串();
对于(int i=1;i
不确定如何处理单词之间的多个空格或条目中奇怪的非字母字符,但这应该考虑主要用例:
import java.util.Scanner;
public class HW2Q1 {
public static void main(String[] args)
{
System.out.println("Enter a sentence");
// get input and convert it to a list
Scanner keyboard = new Scanner(System.in);
String sentence = keyboard.nextLine();
String[] sentenceList = sentence.split(" ");
// iterate through the list and write elements with odd indices to a String
String returnVal = new String();
for (int i = 1; i < sentenceList.length; i+=2) {
returnVal += sentenceList[i] + " ";
}
// print the string to the console, and remove trailing whitespace.
System.out.println(returnVal.trim());
}
}
import java.util.Scanner;
公共类HW2Q1{
公共静态void main(字符串[]args)
{
System.out.println(“输入句子”);
//得到inpu