我有一个文本文件,里面有十行,都包含这个确切的信息“25987 20.7”。所以我的问题是,我怎样才能取第二个数字(20.7)并丢弃第一个数字?
答案 0 :(得分:1)
您可以使用字符串的split函数。
因此,您将完整的行读入字符串变量,例如line
,然后使用带有空格的line.split(" ")
作为参数。
这将返回一个包含两个值的数组,您可以继续使用第二个值。 有关详细信息:Java String.split()
答案 1 :(得分:1)
您询问了farmer, corn, goose and fox problem,并在得到良好答复后删除了您的previous question,这对于遇到类似问题的其他人或者应答者所付出的努力来说是不公平的事情,或者如果他们希望看到您是否使用过任何借用的代码,则为您的教师。为了他们的所有好处,让我发布Daniel Mclam的删除答案,以造福他人:
通常情况下,我不会提供一个措辞不好的问题的解决方案,但如果我理解正确的话,我发现它是一个有趣的问题。请确保下次还提供一些源代码,以便我们看到您尝试过的内容。(查看https://stackoverflow.com/help/how-to-ask,了解如何提出一个好问题)
public static void main(String[] args)
{
//scanner object for user input
Scanner input=new Scanner(System.in);
//define objects
//Use true and false to determine the side
boolean farmer=false;
boolean fox=false;
boolean goose=false;
boolean corn=false;
//Show rules
System.out.println("Welcome to the Farmer, Goose, Fox, Corn Problem.");
System.out.println("The Fox and Goose cannot be left alone.");
System.out.println("The Goose and Corn cannot be left alone.");
System.out.println("You may only bring one object across the river at a time.");
//loop flag
boolean isFinished=false;
while(!isFinished)
{
//show objects on the same side as the farmer
System.out.println("\nThe objects on your side of the river are:");
if(farmer==fox)
{
System.out.println("1.Fox ");
}
if(farmer==goose)
{
System.out.println("2.Goose ");
}
if(farmer==corn)
{
System.out.println("3.Corn ");
}
System.out.println("4.Nothing ");
//get user selection and validate
//just makes sure that the object is on the same side as the farmer
//if true move the object then test if valid after switch
boolean isValidInput=false;
int userSelection;
do
{
System.out.print("\nEnter the number for the object you wish to move:");
userSelection=input.nextInt();
if(userSelection==1)
{
if(fox==farmer)
{
farmer=!farmer;
fox=!fox;
isValidInput=true;
}
}
else if(userSelection==2)
{
if(goose==farmer)
{
farmer=!farmer;
goose=!goose;
isValidInput=true;
}
}
else if(userSelection==3)
{
if(corn==farmer)
{
farmer=!farmer;
corn=!corn;
isValidInput=true;
}
}
else if(userSelection==4)
{
farmer=!farmer;
isValidInput=true;
}
else
{
isValidInput=false;
}
}while(!isValidInput);
//check solution
boolean isValid=false;
if((fox==goose && farmer!=fox))
{
System.out.println("\nFox and goose cannot stay together.");
}
else if(goose==corn && farmer!=goose)
{
System.out.println("\nGoose and corn cannot stay together.");
}
else
{
isValid=true;
}
//if the solution is not valid switch objects back and request user input again
if(!isValid)
{
if(userSelection==1)
{
if(fox==farmer)
{
farmer=!farmer;
fox=!fox;
isValidInput=true;
}
}
else if(userSelection==2)
{
if(goose==farmer)
{
farmer=!farmer;
goose=!goose;
isValidInput=true;
}
}
else if(userSelection==3)
{
if(corn==farmer)
{
farmer=!farmer;
corn=!corn;
isValidInput=true;
}
}
else if(userSelection==4)
{
farmer=!farmer;
isValidInput=true;
}
else
{
isValidInput=false;
}
}
//check if final solution achieved
if(fox && goose && corn && farmer)
{
System.out.println("\nYou Win, Good Job!!!");
isFinished=true;
}
}//end while
} //end main