我正在学习java,我已经在堆栈上编写了一个程序(LIFO)。 代码的目的是从键盘读取整数并构建堆栈。
class StackDemo
{
public static void main(String[] arg)
throws java.io.IOException{
Stack s1 = new Stack();
char ch1;
System.out.println("would you like to push data into stack??? Enter y for yes. Enter n for no");
ch1 = (char) System.in.read();
if(ch1 == 'y')
{
for(int i=0; i<10; i++)
{
int input;
System.out.println("Enter an integer to push to the stack");
input = (int)System.in.read();
s1.push(input);
}
}
else if(ch1 == 'n')
{
}
else
{
System.out.println("push - Enter a valid input");
}
}
}
运行程序时,控制台上会显示以下结果。
----------------------------------------------------------------------------
D:\personal\learn_social\Java\Complete_reference\Programs>java StackDemo
would you like to push data into stack??? Enter y for yes. Enter n for no
y
Enter an integer to push to the stack
Value is pushed onto stack successfully
Enter an integer to push to the stack
Value is pushed onto stack successfully
Enter an integer to push to the stack
----------------------------------------------------------------------------
但预期结果是
-------------------------------------------------------------------------
D:\personal\learn_social\Java\Complete_reference\Programs>java StackDemo
would you like to push data into stack??? Enter y for yes. Enter n for no
y
Enter an integer to push to the stack
-------------------------------------------------------------------------
任何人都可以帮助解决代码中的问题吗?
提前致谢, Nikhil S
答案 0 :(得分:2)
您遇到此问题,因为您直接使用System.in
输入流来读取输入。它不会按照您的意图运行。您可以获得多个输入,因为System.in
会在按下&#39; y&#39;后输入您输入的新行[Enter]。也作为输入。使用System.in
的一种方法是将其包含在Scanner
中,如下所示:
import java.util.Scanner;
import java.util.Stack;
class StackDemo {
public static void main(String[] arg) throws java.io.IOException {
Scanner in = new Scanner(System.in);
Stack s1 = new Stack();
System.out.println("Would you like to push data into stack??? Enter y for yes. Enter n for no");
String ch1 = in.nextLine();
if (ch1.equalsIgnoreCase("y")) {
for (int i = 0; i < 10; i++) {
System.out.println("Enter an integer to push to the stack");
int input = (int) in.nextInt();
s1.push(input);
}
} else if (ch1.equalsIgnoreCase("n")) {
} else {
System.out.println("Push - Enter a valid input");
}
}
}
或者,您可以使用BufferedReader
代替Scanner
。您应该从文档中了解更多信息。您可以找到说明here和here。希望这可以帮助!
答案 1 :(得分:1)
如:System.in.read() does not read
您需要使用Scanner类。您可以像这样实例化扫描仪对象
Scanner scan = new Scanner(System.in);
然后使用
input = scan.nextInt();
答案 2 :(得分:1)
问题在于,当您键入&#39; y&#39;然后按Enter,您实际输入3个字符:y,\ r,\ n(13个墨盒返回和10个换行)。使用上述方法之一可以避免该问题。我会用#Fazovsky写的那个,因为它更简单。
答案 3 :(得分:0)
我建议您定义一个阅读器对象。在您的代码中,可能会出现错误,因为连续两个&#34; read.in&#34;没有关闭读者的操作。以下是样本:
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
String s = br.readLine();
System.out.print("Enter Integer:");
try{
int i = Integer.parseInt(br.readLine());
}catch(NumberFormatException nfe){
System.err.println("Invalid Format!");
}
br.close();
}
}
答案 4 :(得分:0)
使用- (void)writeData:(NSData *)data {
[self.asyncSocket writeData:data withTimeout:-1 tag:1];
}
#pragma mark - GCDAsyncSocket Delegate Methods -
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err {
[[NSNotificationCenter defaultCenter] postNotificationName:KSOCKET_MANAGER_REMOVAL
object:self
userInfo:nil];
}
- (void)socketDidCloseReadStream:(GCDAsyncSocket *)sock {
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
DDLogInfo(@"SOCKET DID READ DATA:%@:%@",self.asyncSocket,self.contactJID);
[sock readDataWithTimeout:-1 tag:0];
if (data!=nil)
[self loadMessageWithData:data];
}
#pragma mark - Private Methods -
- (void)loadMessageWithData:(NSData *)encryptedData {
NSLog(@"DATA RECEIVED");
}
作为int值。
Scanner