从输入字符串获取输入,该输入字符串的值具有差值/间隙“ " [英] Taking input from an input string which has values at a difference/gap of " "

查看:69
本文介绍了从输入字符串获取输入,该输入字符串的值具有差值/间隙“ "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我尝试了什么

我是一个noobie所以想问我可以在下面的代码中做我正在做的事情

我正在练习的问题是在这个链接上

实践| GeeksforGeeks [ ^ ]





  import  java。 util.Scanner; 
class 测试{
public static void main( String args []){
Scanner sc = new扫描器(System.in);

int Testcases = sc.nextInt();
for int i = 0; i< Testcases; i ++){
int len = sc.nextInt();
int a [] = new int [len- 1 ];
for int k = 0; k< len- 1 ; k ++){
a [k] = Integer.parseInt(sc.nextLine()。split( ));
}
for int j = 0; j< len; j ++)
for int k = 0; k< len- 1 ; k ++)
if (a [k] == j){}
else {
System.out.println(j);
}
}}}

我尝试了什么:

到目前为止我尝试了什么
我是一个noobie所以想问我可以我在这里做的事情在下面的代码
我正在练习的问题是在这个链接
我可以 这个使用bufferreader,但是我希望用扫描仪尝试
****这不算作业我一直在练习****

我是收到编译错误,说非法类型 String []无法转换为字符串。

解决方案

 a [k] = Integer.parseInt(sc.nextLine()。split( )); 



String.split 返回一个字符串数组,而不是一个字符串。你需要这样的东西:

  String  [] numbers = sc.nextLine()。split( ); 
for int k = 0; k< len- 1 ; k ++){
a [k] = Integer.parseInt(numbers [k]);
}



还记得要检查你是否输入了完整的 len-1 数字。


what i have tries so far
I am a noobie so wanted to ask can I do what I am doing here in the below code
question I am practicing is on this link
Practice | GeeksforGeeks[^]


import java.util.Scanner;
class Test{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);

int Testcases=sc.nextInt();
for(int i=0;i<Testcases;i++){
   int  len = sc.nextInt();
   int a[] = new int[len-1];
for(int k =0;k<len-1;k++){
a[k] = Integer.parseInt(sc.nextLine().split(" "));
}
   for(int j =0;j<len;j++)
   for(int k =0;k<len-1;k++)
   if(a[k]==j){}
   else{
       System.out.println(j);
   }
}}}

What I have tried:

what i have tries so far
I am a noobie so wanted to ask can I do what I am doing here in the below code
question I am practicing is on this link
I can do this using bufferreader ,but wanted to try with scanner also 
****This isnt homework I am practicing as always****

I am getting compilation error which says illegal type String[] cannot be converted to string.

解决方案

a[k] = Integer.parseInt(sc.nextLine().split(" "));


String.split returns an array of strings, not a single String. You need something like:

String[] numbers = sc.nextLine().split(" ");
for(int k =0; k < len-1; k++) {
    a[k] = Integer.parseInt(numbers[k]);
}


Also remembering to check that you enter the full len-1 numbers.


这篇关于从输入字符串获取输入,该输入字符串的值具有差值/间隙“ &QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆