如何在C++中实现Java "Scanner"?
How to implement Java "Scanner" in C++?
请查看以下java代码
import java.util.Scanner;
public class Main
{
static int mul=1;
static String convert;
static char[] convertChar ;
static StringBuffer buffer = new StringBuffer("");
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
int number=0;
int loopValue = scan.nextInt();
//System.out.println("print: "+loopValue);
for(int i=0;i<loopValue;i++)
{
number = scan.nextInt();
// System.out.println("print: "+number);
for(int a=1;a<=number/2;a++)
{
if(number%a==0)
{
//System.out.println(a);
mul = mul*a;
//System.out.println(mul);
}
}
convert = String.valueOf(mul);
convertChar = convert.toCharArray();
if(convertChar.length>4)
{
/*System.out.print(convertChar[convertChar.length-4]);
System.out.print(convertChar[convertChar.length-3]);
System.out.print(convertChar[convertChar.length-2]);
System.out.print(convertChar[convertChar.length-1]);
System.out.println();*/
buffer.append(convertChar[convertChar.length-4]);
buffer.append(convertChar[convertChar.length-3]);
buffer.append(convertChar[convertChar.length-2]);
buffer.append(convertChar[convertChar.length-1]);
System.out.println(buffer);
}
else
{
System.out.println(mul);
}
//System.out.println(mul);
mul = 1;
}
}
}
这个代码是用来计算给定数的正除数的乘积的。我在这里使用了扫描仪,因为我不知道会输入多少输入。这就是为什么我不能去这样的地方
int a, b;
cin >> a >> b
在C++中。所有的输入都将由一个测试引擎插入到一个类似的中
6 2 4 7 8 90 3456
如何使用C++实现Java"Scanner"?有头文件吗?请帮忙!
您似乎在使用Scanner
从标准输入流中一次读取一个整数。使用提取运算符operator>>
可以很容易地实现这一点。
替换此代码:
Scanner scan = new Scanner(System.in);
int number=0;
int loopValue = scan.nextInt();
//System.out.println("print: "+loopValue);
for(int i=0;i<loopValue;i++)
{
number = scan.nextInt();
// System.out.println("print: "+number);
有了这个:
int number=0;
int loopvalue=0;
std::cin >> loopvalue;
for(int i = 0; i < loopValue; i++)
{
std::cin >> number;
您应该在>>
操作之后检查std::cin
的值,以确保它们成功。
参考:
- http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
- http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
如果使用std::cin >> value;
读取值,则只能在检测到新行后处理整行。
如果你想在键入每个数字时对其进行处理,那么你可以使用以下函数:
int nextInt()
{
std::stringstream s;
while (true)
{
int c = getch();
if (c == EOF) break;
putch(c); // remove if you don't want echo
if ((c >= '0' && c <= '9') || (c == '-' && s.str().length() == 0))
s << (char)c;
else if (s.str().length() > 0)
break;
}
int value;
s >> value;
return value;
}
好吧,可能有更有效的方法来写,但它会逐个字符读取输入,直到遇到数字,并且当遇到数字以外的任何数字时,它会返回读取的任何数字。
例如,1 2 3 4在第一次呼叫时会返回1,在第二次呼叫时返回2。
相关文章:
- 在java中解决这段代码时面临循环中的问题
- 尝试用java代码编译和运行c++代码
- 在这种情况下,java对象是否可以调用本机函数
- 在java中读取c++字节的位字段
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 使用已使用 java 编码的 openssl 解码数据
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- Android Java USB for native cpp
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ equivalent to Java Map getOrDefault?
- C++ 中的 Java 样式枚举
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 加密在 Windows、C++ 和 Java 中传输中的数据
- Java从C++回调到C++回调
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 用C++包装 Java 库 (JNI)
- 使用 TreeSet Java 对反转进行计数
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 如何使用JNI用java调用Scanner API的C方法
- 如何在C++中实现Java "Scanner"?