为什么程序不接受输入就终止?

时间:2021-07-03 21:20:27

标签: arrays c string

这是我的程序示例:

java.lang.ClassCastException: class org.eclipse.wb.internal.core.model.property.Property$1 cannot be cast to class java.awt.Font (org.eclipse.wb.internal.core.model.property.Property$1 is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @6980fa39; java.awt.Font is in module java.desktop of loader 'bootstrap')
    at org.eclipse.wb.internal.swing.model.property.editor.font.DerivedFontPage.configure(DerivedFontPage.java:271)
    at org.eclipse.wb.internal.swing.model.property.editor.font.FontPropertyEditor.openDialog(FontPropertyEditor.java:100)
    at org.eclipse.wb.internal.core.model.property.editor.TextDialogPropertyEditor$1.onClick(TextDialogPropertyEditor.java:35)
    at org.eclipse.wb.internal.core.model.property.editor.presentation.ButtonPropertyEditorPresentationImpl$4.handleEvent(ButtonPropertyEditorPresentationImpl.java:124)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
    at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:654)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1462)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1435)


Full context stack trace:
java.lang.ClassCastException: class org.eclipse.wb.internal.core.model.property.Property$1 cannot be cast to class java.awt.Font (org.eclipse.wb.internal.core.model.property.Property$1 is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @6980fa39; java.awt.Font is in module java.desktop of loader 'bootstrap')
    at org.eclipse.wb.internal.swing.model.property.editor.font.DerivedFontPage.configure(DerivedFontPage.java:271)
    at org.eclipse.wb.internal.swing.model.property.editor.font.FontPropertyEditor.openDialog(FontPropertyEditor.java:100)
    at org.eclipse.wb.internal.core.model.property.editor.TextDialogPropertyEditor$1.onClick(TextDialogPropertyEditor.java:35)
    at org.eclipse.wb.internal.core.model.property.editor.presentation.ButtonPropertyEditorPresentationImpl$4.handleEvent(ButtonPropertyEditorPresentationImpl.java:124)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
    at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:654)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1462)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1435)

在这个程序中,只有在 #include <stdio.h> void sum(); int main() { char choice[4]; do { sum(); printf("\nDo You want to restart the program: yes or no:\n"); fgets(choice, 4, stdin); //error point } while (choice[0] == 'y' || choice[0] == 'Y'); printf("\nThanking You"); return 0; } void sum() { int a = 3, b = 4; printf("sum of two number is %d", a + b); } 的第 1 次迭代中,它才会在 while 中要求输入,并且在下一次迭代中,程序会通过取 choice 中的任何值自动终止。

以下是代码执行后的结果:

choice

我无法理解它在 sum of two number is 7 Do You want to restart the program: yes or no: yes sum of two number is 7 Do You want to restart the program: yes or no: Thanking You [Program finished] 中输入,而我还没有使用 choice(它在缓冲区中留下换行符)。可能是它从缓冲区输入可能是空格或其他字符,但我不知道它是从哪里来的?

2 个答案:

答案 0 :(得分:3)

您的程序在第一个提示后在输入缓冲区中留下一个换行符,因为缓冲区中没有足够的空间。

在第一次调用 fgets 时,您给出字符串“yes”,后跟一个换行符。缓冲区的大小为 4,您将该大小传递给 fgets,因此它最多读取那么多字符 - 1 以说明终止的空字节。所以缓冲区中还有一个换行符。

下次调用 fgets 时会立即读取该换行符。

您应该扩大缓冲区以处理更多字符。

答案 1 :(得分:3)

您的 choice 数组不够大,无法容纳第一个循环的所有输入。 fgets 函数 (4) 的第二个参数告诉它读取至多 3 个字符(然后在输入中附加一个 nul 终止符) .因此,它将换行符留在输入流中,并在第二个循环中(单独)读取。

只需将您的 choice 数组大小和输入限制增加到 5(或更多):

#include <stdio.h>
void sum();
int main()
{
    char choice[5]; // Increase space for newline and nul
    do {
        sum();
        printf("\nDo You want to restart the program: yes or no:\n");
        fgets(choice, 5, stdin); // Inputs 'y', 'e', 's' and newline (4 chars) and appends a nul.
    } while (choice[0] == 'y' || choice[0] == 'Y');
    printf("\nThanking You");
    return 0;
}
void sum()
{
    int a = 3, b = 4;
    printf("sum of two number is %d", a + b);
}

来自fgets - cppreference(我的粗体):

<块引用>

从给定的文件流中读取最多计数 - 1 个字符,并且 将它们存储在 str 指向的字符数组中。解析停止,如果 找到一个换行符,在这种情况下 str 将包含 换行符,或者是否出现文件​​结束符。如果读取字节并且没有 发生错误,立即在该位置写入空字符 在写入 str 的最后一个字符之后。