英文:
Finding character at particular location of String
问题
我理解了,这是你的代码部分:
import java.util.* ;
public class binomial_expansion
{
public static long factorial(long x)
{
long xf = x ;
for(long i = 1; i<=x ; i++)
{
xf*=i ;
}
return xf ;
}
public static double C(long n, long r)
{
double c = factorial(n)/(factorial(n-r)*factorial(r)) ;
return c ;
}
public static void main()
{
Scanner sc = new Scanner(System.in) ;
boolean error = false ;
String a = "" ;
do {
if(error)
{
System.out.println("Error; Please try again") ;
error = false ;
}
System.out.println("Enter a (separate single variable with space) : ") ;
a = sc.nextLine() ;
if(a=="") error = true ;
} while(error) ;
sc.close() ;
Scanner scan = new Scanner(a) ;
long x = scan.nextLong() ;
long x1 = scan.next().charAt(0) ;
}
}
在使用 .next().charAt(index)
创建字符变量时,问题在于字符串中字符的索引号(单个变量)未知(整数的长度未知),并且还需要省略用户输入的用于分隔整数文字和字符文字的空格。如何创建包含单个数字变量的字符变量?
英文:
As a part of a Binomial expansion program, I created a Scanner class (sc
) on an entered number + variable, and another (scan
)on the first term (a in (a+b)<sup>n</sup>)
Here's my program till now :
import java.util.* ;
public class binomial_expansion
{
public static long factorial(long x)
{
long xf = x ;
for(long i = 1; i<=x ; i++)
{
xf*=i ;
}
return xf ;
}
public static double C(long n, long r)
{
double c = factorial(n)/(factorial(n-r)*factorial(r)) ;
return c ;
}
public static void main()
{
Scanner sc = new Scanner(System.in) ;
boolean error = false ;
String a = "" ;
do {
if(error)
{
System.out.println("Error; Please try again") ;
error = false ;
}
System.out.println("Enter a (separate single variable with space) : ") ;
a = sc.nextLine() ;
if(a=="") error = true ;
} while(error) ;
sc.close() ;
Scanner scan = new Scanner(a) ;
long x = scan.nextLong() ;
long x1 = scan.next().charAt(0) ;
}
}
<br>
The problem with creating a character variable using .next().charAt(index)
is that the index number of the character (the single variable) is not known in the string (the length of the integer is not known), and also that the blank space has to be omitted which was entered by the user to separate the integer literal and the character literal. How can the character variable containing the single-digit variable be made ?
答案1
得分: 1
你可以读取一个字符串,然后使用StringTokenizer按空格分隔。然后使用nextToken()来分别获取整数和变量。
使用Long.parseLong()
将字符串转换为长整型。
import java.util.*;
class test{
public static void main(String args[]){
String str = "12 w";
StringTokenizer st = new StringTokenizer(str," ");
int x = Integer.parseInt(st.nextToken());
str=st.nextToken();
System.out.println(str+"-"+x);
}
}
这里我使用了int,你也可以使用long。
英文:
You can read a string and then use stringtokenizer to seperate with black spaces. Then use nextToken() to get the integer and variable seperately.
Long.parseLong()
to convert string to long
import java.util.*;
class test{
public static void main(String args[]){
String str = "12 w";
StringTokenizer st = new StringTokenizer(str," ");
int x = Integer.parseInt(st.nextToken());
str=st.nextToken();
System.out.println(str+"-"+x);
}
}
Here I used int, you can use long aswell
答案2
得分: 1
你只需要一个 Scanner
来获取输入,你可以使用 a = sc.nextLine()
来实现。
接下来,找到空格的索引,然后使用 String.substring
方法来提取每个值。
Scanner sc = new Scanner(System.in);
boolean error = false;
String a = "";
do {
if (error) {
System.out.println("错误; 请重试");
error = false;
}
System.out.println("输入 a (用空格分隔的单个变量):");
a = sc.nextLine();
} while (error);
sc.close();
int indexOf = a.indexOf(' ');
long number = Long.parseLong(a.substring(0, indexOf));
char variable = a.charAt(indexOf + 1);
希望这有帮助!
英文:
You'll only need one Scanner
to get the input, which you do with a = sc.nextLine()
.
From there, find the index of the space, and use the String.substring
method to derive each value.
Scanner sc = new Scanner(System.in) ;
boolean error = false ;
String a = "" ;
do {
if(error)
{
System.out.println("Error; Please try again") ;
error = false ;
}
System.out.println("Enter a (separate single variable with space) : ") ;
a = sc.nextLine() ;
} while(error) ;
sc.close() ;
int indexOf = a.indexOf(' ');
long number = Long.parseLong(a.substring(0, indexOf));
char variable = a.charAt(indexOf + 1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论